제출 #1225093

#제출 시각아이디문제언어결과실행 시간메모리
1225093jfioashfn333Garage (IOI09_garage)C++20
100 / 100
1 ms328 KiB
#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <cmath>
#include <cstring>
#include <set>
#include <queue>
#define int long long
#define ff first
#define ss second
#define pb push_back
#define pp pop_back
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define r0 return 0
using namespace std;
const int N = 1e6 + 5, M = 5001, MOD = 998244353, modu = 1e9 + 7;
int n,m,k,a,b,c,d,x,y;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    
    vector<int> par(n);
    vector<int> w(m);
    
    for(int j = 0; j < n; j++) cin >> par[j];
    for(int j = 0; j < m; j++) cin >> w[j];
    int tt = 2 * m;
    int ans = 0,inp;
    
    queue<int> cqr;
    vector<bool> occ(n, false);
    vector<int> pared(m, -1);
    
    while(tt--) {
        cin >> inp;

        if(inp > 0) {
            bool done = false;
            for(int i = 0; i<n; i++) {
                if(occ[i] == false) {
                    occ[i] = true;
                    pared[inp-1] = i+1;
                    done = true;
                    ans += w[inp-1] * par[i];
                    break;
                }
            }

            if(done == false) {
                cqr.push(inp);
            }
        }

        else {
            inp = (-1) * (inp);
            occ[pared[inp - 1] - 1] = false;
            
            if(cqr.empty() == false) {
                ans += par[pared[inp - 1] - 1] * w[cqr.front() - 1];
                occ[pared[inp - 1] - 1] = true;
                pared[cqr.front() - 1] = pared[inp - 1];
                cqr.pop();
            }
        }
    }

    cout << ans << endl;
    
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...