题解 | #最大上升子序列和#
最大上升子序列和
https://www.nowcoder.com/practice/dcb97b18715141599b64dbdb8cdea3bd
#include<iostream> #include<cstdio> using namespace std; int a[1010]; int dp[1010]; int main() { int n; int answer; scanf("%d", &n); for (int i = 0 ; i < n ; ++i) { scanf("%d", &a[i]); } for (int i = 0 ; i < n ; ++i) { dp[i] = a[i]; for (int j = 0 ; j < i; ++j) { if(a[i] > a[j]){ dp[i] = max(dp[i], dp[j]+a[i]); } } answer = max(answer, dp[i]); } printf("%d\n", answer); }