#P1133. 教主的花园

教主的花园

Description

The leader has a circular garden and wants to plant nn trees evenly around it. However, the soil is special; each position suits different kinds of trees, and some trees may lose aesthetic value if placed in unsuitable soil.

The leader's three favorite kinds of trees have heights 10,20,3010,20,30. He wants the ring of trees to have a sense of layering: at every position, the tree must be strictly higher than both of its adjacent trees or strictly lower than both. Under this condition, design a plan that maximizes the sum of aesthetic values.

Input Format

The first line contains a positive integer nn, the number of trees to plant.

The next nn lines each contain three positive integers ai,bi,cia_i,b_i,c_i not exceeding 1000010000, giving, in clockwise order, the aesthetic value obtained by planting at position ii a tree of height 10,20,3010,20,30, respectively.

The tree at position ii is adjacent to the tree at position i+1i+1; in particular, the tree at position 11 is adjacent to the tree at position nn.

Output Format

Output a single positive integer, the maximum total aesthetic value.

4 
1 3 2 
3 1 2 
3 1 2 
3 1 2

11

Hint

[Sample Explanation].

Plant trees of heights 20,10,30,1020,10,30,10 at positions 11 through nn, respectively, to achieve the highest value.

[Constraints].

  • For 20%20\% of the testdata, n10n \le 10.
  • For 40%40\% of the testdata, n100n \le 100.
  • For 60%60\% of the testdata, n1000n \le 1000.
  • For 100%100\% of the testdata, 4n1054 \le n \le 10^5, and nn is guaranteed to be even.

Translated by ChatGPT 5