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.
![](https://i-blog.csdnimg.cn/blog_migrate/b69a36fbfe92ef60e13d861294653386.jpeg)
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 across many 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 2
4 1 2 3 4
4 3 3 2 1
0
Sample Output
4
10
3
这道题的大概意思是给你一个数n,然后给你n个数,让你计算在这n个数中上升子序列(可以非连续)的最大和。有多组输入,n为零时结束输入。
思路:这是一道简单的的dp题,题目数据很小,找出状态转移方程就行,这里就不多说了,上代码。
代码如下:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
//存数字
int a[1005];
//存到当前位置的上升子序列的最大值
int b[1005];
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
//找出到当前数的上升子序列的最大值
for(int i=0;i<n;i++)
{
b[i]=a[i];
for(int j=0;j<i;j++)
//状态转移方程,当某一点的值小于当前点的值,且到那一点的上升子序列的最大值加上当前点的值大于到当前点的上升子序列的最大和,更新
if(a[i]>a[j]&&a[i]+b[j]>b[i])
b[i]=a[i]+b[j];
}
//寻找最大值
int Max=b[0];
for(int i=0;i<n;i++)
if(Max<b[i])
Max=b[i];
printf("%d\n",Max);
}
return 0;
}