Description
In other words, the RP in the world is conservative. If your RP is high, my RP may be low. If your RP is high today, the RP may be low tomorrow. Our RP is always changing in height, height, and height. Some things can increase the RP, and some things will decrease the RP.
After finding this truth, wujitu decided to accumulate more characters before the final exam. He found that everything he does has the following impact on RP:
(1) The first thing he did was to increase Rp.
(2) If the last thing he did was to increase RP, then the next thing he did was to decrease Rp.
(3) If the last thing he did was to reduce the RP, then the next thing he did was to increase the RP.
Wujitu's current accumulated character is 0. The RP value increases, and the RP value decreases.
Wujitu has n tasks to do before the final exam, and each task has its own RP coefficient. For these tasks, wujitu can only have two options: Do, do not do. The order of things is certain, that is to say, wujitu can only choose to do it in the given order.
Please help wujitu accumulate the most RP before the final exam.
-
Input
-
Each group of data contains two rows. The first row has an integer n (n <= 1000000), which indicates there are n tasks. The second row has n integers, indicates the RP value of each task Ri [positive number]
RI <= 1000000
-
Output
-
The output result contains only one integer, that is, the maximum Rp value that wujitu can accumulate before the end of the period.
-
Sample Input
-
1141 3 1 2
-
Sample output
-
14
Solution:
This is actually a dynamic planning question. It uses two arrays to store the consequences of doing something or not. Finally, compare the value of the array. Each step is associated with the next step.
# Include <stdio. h> int RP [1000001]; Main () {long number; int I; long f [2], G [2]; while (scanf ("% LLD ", & number )! = EOF) {If (number = 0) printf ("0 \ n"); If (number> 0) {for (I = 1; I <= number; I ++) scanf ("% d", & RP [I]); F [0] = 0; G [0] = 0; for (I = 1; I <= number; I ++) {If (F [(I-1) % 2] <G [(I-1) % 2] + RP [I]) f [I % 2] = G [(I-1) % 2] + RP [I]; else f [I % 2] = f [(I-1) % 2]; if (G [(I-1) % 2] <F [(I-1) % 2]-RP [I]) g [I % 2] = f [(I-1) % 2]-RP [I]; else G [I % 2] = G [(I-1) % 2];} if (G [Number % 2]> F [Number % 2]) f [Number % 2] = G [Number % 2]; printf ("% LLD \ n ", f [Number % 2]) ;}}