Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.6 kB
1
Indexable
#include <bits/stdc++.h>

using namespace std;
 
#define triple tuple<int, int, int>
#define INF 1e9+1

void show(triple el) {
    cout << "(" << get<0>(el) << ", " << get<1>(el) << ", " << get<2>(el) <<  ")\n";
}

int main() {

    int n; cin >> n;
    auto cmp = [](triple a, triple b) {return get<1>(a) < get<1>(b); };
    vector<triple> projs;
    int minend = INF, maxend = 0;
    for(int i = 0; i < n; i++) {
        int sv, ev, vv; 
        cin >> sv >> ev >> vv; 
        projs.push_back({sv, ev, vv});
        maxend = max(maxend, ev);
        minend = min(minend, ev);
    }

    sort(projs.begin(), projs.end(), cmp);

    // NOTE: Sorted decreasing by ending time!
    // for (auto el : projs) {
    //     show(el);
    // }

    // dp[i] = max money you can earn working till day i inclusive
    vector<int> dp(maxend+1, 0);
    int current_proj = 0;
    for (int i = minend; i <= maxend; i++) {

        dp[i] = dp[i-1];
        // cout << i << " " << dp[i] << "\n";
        // show(el);
        
        triple el = projs[current_proj];
        int start = get<0>(el);    
        int end   = get<1>(el);    
        int value = get<2>(el);     

        while (end == i) {
            dp[i] = max(dp[i], dp[start-1] + value);
            current_proj++;

            el = projs[current_proj];
            start = get<0>(el);    
            end   = get<1>(el);    
            value = get<2>(el);        
        }
    }

    // for (auto el : dp) {
    //     cout << el << " ";
    // }
    // cout << "\n";

    cout << dp[maxend];

}
Leave a Comment