Submission #420780

#TimeUsernameProblemLanguageResultExecution timeMemory
420780chirathnirodhaGarage (IOI09_garage)C++17
100 / 100
2 ms332 KiB
//Coded by Chirath Nirodha
#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define I insert
#define P push
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_tree;
const ll mod=1e9+7;
inline void io(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
void solve(){
    io();
    int n,m;cin>>n>>m;
    int r[n],w[m];
    for(int i=0;i<n;i++)cin>>r[i];
    for(int i=0;i<m;i++)cin>>w[i];
    bool park[n];memset(park,false,sizeof(park));
    int place[m];
    ll ans=0;
    queue<int> q;
    for(int i=0;i<2*m;i++){
        int x;cin>>x;
        if(x>0){
            x--;
            bool found=false;
            for(int j=0;j<n;j++)
                if(!park[j]){
                    park[j]=true;
                    place[x]=j;
                    ans+=w[x]*r[j];
                    found=true;
                    break;
                }
            if(!found)q.P(x);  
        } 
        else{
            x++;x*=-1;
            park[place[x]]=false;
            if(!q.empty()){
                int c=q.front();q.pop();
                park[place[x]]=true;
                place[c]=place[x];
                ans+=w[c]*r[place[x]];
            }
        }
    }
    cout<<ans<<endl;
}
int main(){
    io();
    solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...