最大上升子序列和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> #include <algorithm> #include <unordered_map> #include <vector> using namespace std; typedef pair<int, int> pii; const int N = 5010; pii v[N]; int A[N]; int dp[N]; int main() { int n; int res = 0; cin >> n; for(int i = 1; i <= n; i++) { cin >> A[i]; } for(int i = 1; i <= n; i++) { dp[i] = A[i]; for(int j = 1; j < i; j++) { if (A[j] < A[i]) { dp[i] = max(dp[i], dp[j] + A[i]); } } res = max(res, dp[i]); } cout << res << endl; return 0; } |