Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Maximum Sum Increasing Subsequence

#include <bits/stdc++.h>
using namespace std;

void solve(vector<long long> &a) {
    int n = a.size();

    vector<long long> dp(n);
    // dp[i] is the maximum sum increasing subsequence ending at i.

    for (int i = 0; i < n; i++) {
        dp[i] = a[i];
        for (int j = i - 1; j >= 0; j--) {
            if (a[i] > a[j]) {
                dp[i] = max(dp[i], a[i] + dp[j]);
            }
        }
    }

    cout << *max_element(dp.begin(), dp.end()) << endl;
}

int main() {
    int n;
    cin >> n;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    solve(a);
    return 0;
}