//Code By :d:3:m:o:r:a:l:1:z:e:r
#include <bits/stdc++.h>
using namespace std;
// sum(n^2) = n(n+1)(2n+1)/6
// GP - (bk-a)/k-1
// Faulhaber’s formula
typedef long long ll;
typedef long double lld;
typedef vector<int> vi;
typedef pair<int,int> pi;
string s,t;
ll ans = 0;
int INF=1e9;
int temp;
map<char,int> mpp;
string chars="abcdefghijklmnopqrstuvwxyz";
unordered_map<char, int> charToInt;unordered_map<int, char> intToChar;
#define PB push_back
#define nl '\n'
#define vectorIn(vec_name, size) \
vector<ll> vec_name(size); \
for (int i = 0; i < size; ++i) { \
cin >> vec_name[i]; \
}
#define vectorHash(vec_name, size) \
vector<ll> vec_name(size); \
map<int, int> mpp; \
for (int i = 0; i < size; ++i) { \
cin >> vec_name[i]; \
mpp[vec_name[i]]++; \
}
void print(vector<int> &vec_name){
for (int i = 0; i < vec_name.size(); ++i) {
cout<<vec_name[i]<<" ";
}
cout<<nl;
}
// void createMapping(){
// for(int i=0;i<chars.size();++i)charToInt[chars[i]]=i+1;
// for(auto const& [s, i] : charToInt)intToChar[i] = s;
// }
ll sum(vector<ll> numbers){
return accumulate(numbers.begin(),numbers.end(),0LL);
}
void stringHash(string s){
for(int c:s)mpp[c]++;
}
ll gcd(ll a,ll b){while(b)a%=b,swap(a,b);return a;}
ll lcm(ll a,ll b){return(a&&b)?(a/gcd(a,b)*b):0;}
void solve() {
int n,m;cin>>n>>m;
vector<int> cost(n);
vector<int> weight(m);
for(int i=0;i<n;i++)cin>>cost[i];
for(int i=0;i<m;i++)cin>>weight[i];
ll sm=0;
vector<int> open(n,-1);
ll openamt=n;
ll q=2*m;
queue<int> qe;
while(q--){
ll no,flag=-1; cin>>no;
if(no > 0) {
qe.push(no-1);
} else {
int target_car = abs(no) - 1;
for(int i = 0; i < n; i++) {
if(open[i] == target_car) {
open[i] = -1;
openamt++;
break;
}
}
}
while(!qe.empty() && openamt > 0) {
int best_spot = -1;
for(int i = 0; i < n; i++) {
if(open[i] == -1) {
best_spot = i;
break;
}
}
if(best_spot != -1) {
int car_to_park = qe.front();
qe.pop();
open[best_spot] = car_to_park;
openamt--;
sm += (ll)cost[best_spot] * weight[car_to_park];
} else {
break;
}
}
}
cout<<sm;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
// int tt; cin>>tt;
// while(tt--){
solve();
// }
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |