题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087
就是求最长上升子序列,但是结果求的是该序列的和,转移方程: dp[i] = max(dp[i], dp[j]+a[i])
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <algorithm> 5 #include <iostream> 6 #include <cmath> 7 #include <queue> 8 #include <map> 9 #include <set> 10 #include <stack> 11 #include <list> 12 #include <vector> 13 14 using namespace std; 15 16 inline int max(int a, int b) { 17 return a > b ? a : b; 18 } 19 const int maxn = 1010; 20 int n; 21 int a[maxn]; 22 int dp[maxn]; 23 int ans; 24 int main() { 25 // freopen("in", "r", stdin); 26 while(~scanf("%d", &n) && n) { 27 ans = 0; 28 for(int i = 0; i < n; i++) { 29 scanf("%d", &a[i]); 30 } 31 for(int i = 0; i < n; i++) { 32 dp[i] = a[i]; 33 for(int j = 0; j < i; j++) { 34 if(a[j] < a[i]) { 35 dp[i] = max(dp[i], dp[j] + a[i]); 36 } 37 } 38 ans = max(ans, dp[i]); 39 } 40 printf("%d ", ans); 41 } 42 }