이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<vector>
#include<iostream>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
int N;
int main(){
cin >> N;
vector<int> a(N), b(N);
queue<int> QA;
queue<int> QB;
for (int i=0;i<N;i++){
cin >> a[i] >> b[i];
QA.push(a[i]);
QB.push(b[i]);
}
long long cost = 0;
long long overflow = 0;
long long overflow_location = 0;
while (!QA.empty() || !QB.empty()){
if (overflow == 0 && QA.empty()) break;
if (overflow == 0){
overflow = QA.front();
overflow_location = N - QA.size();
QA.pop();
continue;
}
if (overflow > 0){
long long current_location = N - QB.size();
long long fixed = min(overflow, (long long)QB.front());
overflow -= QB.front();
QB.pop();
cost += abs(current_location - overflow_location) * fixed;
if (overflow < 0) overflow_location = current_location;
} else {
long long current_location = N - QA.size();
long long fixed = min(-overflow, (long long) QA.front());
overflow += QA.front();
QA.pop();
cost += abs(current_location - overflow_location) * fixed;
if (overflow > 0) overflow_location = current_location;
}
}
cout << cost << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |