HDU 1087 Super Jumping! Jumping! Jumping! (DP), hdu1087

Source: Internet
Author: User

HDU 1087 Super Jumping! Jumping! Jumping! (DP), hdu1087
C-Super Jumping! Jumping! Jumping!Time Limit:1000 MSMemory Limit:32768KB64bit IO Format:% I64d & % I64u

Description

Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping !" Is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.



The game can be played by two or more than two players. it consists of a chessboard and some chessmen, and all chessmen are marked by a positive integer or "start" or "end ". the player starts from start-point and must jumps into end-point finally. in the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum .). and all players cannot go backwards. one jumping can go from a chessman to next, also can go into SS unzip chessmen, and even you can straightly get to end-point from start-point. of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. note that your score comes from the sum of value on the chessmen in you jumping path.
Your task is to output the maximum value according to the given chessmen list.

Input

Input contains multiple test cases. Each test case is described in a line as follow:
N value_1 value_2... Value_N
It is guarantied that N is not more than 1000 and all value_ I are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.

Output

For each case, print the maximum according to rules, and one line one case.

Sample Input

3 1 3 24 1 2 3 44 3 2 10

Sample Output

4103 is to find the largest ascending substring, can be discontinuous, but s [I] must be greater than s [I-1]. The idea is to calculate from the end and find out the sum of the largest ascending substring starting from this position and store it in the DP array. For example, if there are only 1 2 99 97 98 elements (and this is also a group of error-prone data), the calculation starts from 98, the largest ascending substring of 98 and apparently 98, so DP [4] = 98. Then start to calculate 97, The 97 algorithm is to find out all the greater values behind it, and then compare their DP values, take the largest one, and add 97.Because there is only one 98,98 greater than 97, the DP value of 97 is 97 + 98, that is, the ascending substring starting from 97 is 97 98. If we calculate 99, all the numbers below will be smaller than it, which is not desirable. Therefore, the DP value is itself. Calculate 2 again. Obviously, all the numbers below are larger than it, so we can take them, but we need to take the maximum DP value, that is, 97 (195). Likewise, the same is true for 1. The DP value of 1 is 198, and the maximum value is. To the general situation, we need to traverse the DP array after finally calculating the answer, because the final answer may not start with the first element.
1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <string. h> 4 # define MAX 1005 5 6 int main (void) 7 {8 int n; 9 int s [MAX]; 10 int dp [MAX], max, ans; // dp [I] stores the largest ascending substring starting with I and 11 12 while (scanf ("% d", & n) 13 {14 for (int I = 0; I <n; I ++) 15 scanf ("% d", & s [I]); 16 17 ans = dp [n-1] = s [n-1]; 18 for (int I = n-2; I> = 0; I --) 19 {20 max = 0; 21 for (int j = I + 1; j <n; j ++) // check which substring is next to s [I ]. Obtain the 22 if (s [I] <s [j] & max <DP [j]) 23 max = dp [j]; 24 dp [I] = s [I] + max; 25 26 ans = ans <dp [I]? Dp [I]: ans; 27} 28 ans = ans <0? 0: ans; // note that the end point can be directly jumped from the start point. The value is 0, if the final value is negative, select this scheme 29 30 printf ("% d \ n", ans); 31} 32 33 return 0; 34}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.