Under construction

Still being built. Things may change

Points

Interview

Time limit3sMemory limit128 MB

Summary
Pick a subset of targets in a row to maximize the total where each picked target scores based on how many neighbors are also picked.
Level

Medium5 of 10

Topics
Dynamic programming
Solved
No attempts yet

Problem

John likes amusement parks. He goes every weekend and plays a different game. This weekend he found a hard one, a target shooting game.

The nn targets stand in a row along a straight line, numbered from 11 to nn from right to left. John picks the targets he wants to shoot, and every target he picks scores points. The score of target ii depends on how many of its neighbors, target i−1i-1 and target i+1i+1, John picked as well.

  • aia_i points if he picked neither neighbor
  • bib_i points if he picked exactly one neighbor
  • cic_i points if he picked both neighbors

Target 11 and target nn have a single neighbor each, so they never have two picked neighbors. A target John does not pick scores nothing. Find the largest total score John can reach.

Input

The first line contains the number of targets nn. (n<1000000n < 1000000)

Each of the next nn lines contains the values aia_i, bib_i, and cic_i of target ii, separated by spaces, in order from i=1i = 1 to i=ni = n. (0≤ai,bi,ci≤10000 \le a_i, b_i, c_i \le 1000)

Output

Print the maximum number of points John can win on a single line.

Hint

The input is always well formed, and the file ends right after the last target. One input holds exactly one set of targets. Print the result to standard output from the beginning of a line.

Examples2

  1. Example 1

    Input
    1
    3 0 0
    
    Expected output
    3
    
  2. Example 2

    Input
    1
    1 2 3
    
    Expected output
    1