1. 程式人生 > >hdu - 1087 Super Jumping! Jumping! Jumping!(dp)

hdu - 1087 Super Jumping! Jumping! Jumping!(dp)

題目:http://acm.hdu.edu.cn/showproblem.php?pid=1087

 

 

題意:n個數組成的序列中,求最大遞增子序列的和。

 

 

dp思路:和最長遞增子序列類似,只不過dp陣列中儲存的是序列和而不是序列長度,轉移方程為dp=max(dp[i],dp[j]+a[i])。

 

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <set>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 10;
ll dp[maxn];
ll a[maxn];
int main(){
	int n;
	while (cin >> n && n){
		for (int i = 1; i <= n; i++)
			cin >> a[i];
		ll ans = a[1];
		for (int i = 1; i <= n; i++){
			dp[i] = a[i];
			for (int j = 1; j < i; j++){
				if (a[i] > a[j])
					dp[i] = max(dp[i], dp[j] + a[i]);
			}
			ans = max(ans, dp[i]);
		}
		cout << ans << endl;
	}
}