Submission #1171089

#TimeUsernameProblemLanguageResultExecution timeMemory
1171089bbartekTwo Dishes (JOI19_dishes)C++20
15 / 100
75 ms63304 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define st first
#define nd second
#define pb push_back

const int maxn = 1e6+7;

ll czas1[maxn];
ll czas2[maxn];
ll prog1[maxn];
ll prog2[maxn];
ll nagroda1[maxn];
ll nagroda2[maxn];

ll dp[2003][2003];
ll czas[2003][2003];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n,m;
    cin>>n>>m;

    ll a,b,c;
    for(int i=1;i<=n;i++){
        cin>>a>>b>>c;
        czas1[i] = a;
        prog1[i] = b;
        nagroda1[i] = c;
    }
    for(int i=1;i<=m;i++){
        cin>>a>>b>>c;
        czas2[i] = a;
        prog2[i] = b;
        nagroda2[i] = c;
    }

    if(n<=2000 && m<=2000){
        for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                dp[i][j] = -1e18;
                if(i>0){
                    czas[i][j] = czas[i-1][j] + czas1[i];
                }
                else if(j>0){
                    czas[i][j] = czas[i][j-1] + czas2[j];
                }
                if(i>0 && czas[i][j] <= prog1[i]){
                    dp[i][j] = max(dp[i][j],dp[i-1][j] + nagroda1[i]);
                }
                else{
                    dp[i][j] = max(dp[i][j],dp[i-1][j]);
                }

                if(j>0 && czas[i][j] <= prog2[j]){
                    dp[i][j] = max(dp[i][j],dp[i][j-1] + nagroda2[j]);
                }
                else{
                    dp[i][j] = max(dp[i][j],dp[i][j-1]);
                }
            }
        }
        cout<<dp[n][m]<<"\n";
        return 0;
    }
    ll licz=0;
    ll wyn = 0;
    ll maks = -1e18;

    ll kon = n;
    for(int i=1;i<=n;i++){
        if(licz + czas1[i] > prog1[1]){
            kon = i-1;
            break;
        }
        licz += czas1[i];
        wyn += nagroda1[i];
    }

    int x=1;
    while(x<=m && licz + czas2[x] <= prog1[1]){
        wyn += nagroda2[x];
        licz += czas2[x];
        x++;
    }
    maks = max(maks,wyn);
    for(int i=kon;i>=1;i--){
        licz -= czas1[i];
        wyn -= nagroda1[i];
        while(x<=m && licz + czas2[x] <= prog1[1]){
            wyn += nagroda2[x];
            licz += czas2[x];
            x++;
        }
        maks = max(maks,wyn);
    }

    cout<<maks<<"\n";

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...