Submission #1329437

#TimeUsernameProblemLanguageResultExecution timeMemory
1329437liptonekBikeparking (EGOI24_bikeparking)C++20
100 / 100
32 ms7468 KiB
#include <bits/stdc++.h>
using namespace std;

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

    int n;
    cin>>n;

    vector<long long> x(n);

    for(int i=0; i<n; i++) 
    {
        cin>>x[i];
    }

    vector<long long> y(n);
    
    long long users=0;
    
    for(int i=0; i<n; i++) 
    {
        cin>>y[i];
    
        users+=y[i];
    }

    vector<long long> u(n,0);

    long long assigned=0;

    for(int i=0; i<n; i++) 
    {
        long long take=min(x[i],users-assigned);
    
        u[i]=take;
        assigned+=take;
    
        if(assigned==users) 
        {
            break;
        }
    }

    int sl=0;
    int sh=n-1;
    int tl=0;
    int th=n-1;
    
    long long score=0;

    while(sl<=sh && tl<=th) 
    {
        while(sl<=sh && y[sl]==0) 
        {
            sl++;
        }

        while(sl<=sh && y[sh]==0) 
        {
            sh--;
        }

        while(tl<=th && u[tl]==0) 
        {
            tl++;
        }

        while(tl<=th && u[th]==0) 
        {
            th--;
        }

        if(sl>sh || tl>th) 
        {
            break;
        }

        if(sh>th) 
        {
            long long count=min(y[sh],u[th]);
        
            score+=count;
        
            y[sh]-=count;
            u[th]-=count;
        } 
        else if(sl>tl) 
        {
            long long count=min(y[sl],u[tl]);

            score+=count;

            y[sl]-=count;
            u[tl]-=count;
        } 
        else 
        {
            long long count=min(y[sl],u[th]);

            if(sl<th) 
            {
                score-=count;
            }

            y[sl]-=count;
            u[th]-=count;
        }
    }

    cout<<score<<endl;

    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...