All problems

Problem 753

Ninja's Training

Medium · 20 pts · Dynamic Programming

Problem

A ninja trains for N days, and each day does exactly one of three activities (running, fighting practice or learning new moves), earning given points for it. The ninja may not do the same activity on two consecutive days. Find the maximum total points. Let best[d][a] be the most points over the first d days when day d's activity is a; it equals points[d][a] plus the larger of best[d-1] for the two other activities.

Input

The first line contains N. Each of the next N lines contains three integers: the points for the three activities on that day.

Output

Print the maximum total points.

Constraints

1 <= N <= 100000; 0 <= points <= 100

Example input

3
1 2 5
3 1 1
3 3 3

Example output

11

Explanation

For days 1 2 5 / 3 1 1 / 3 3 3, pick activity 3 on day 1 (5), activity 1 on day 2 (3) and activity 2 or 3 on day 3 (3). The total 11 is the maximum.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Swim in Rising Water Next: Subset Sum Equal to Target