#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
int up = 0, down = 0;
vector<int> v;
for (int i = 0; i < n; i++) {
while (!v.empty()) {
// assign as many users as possible to the nearest better slot than their chosen one
if (b[i] == 0) break;
if (a[v.back()] == 0) {
v.pop_back();
continue;
}
int temp = min(a[v.back()], b[i]);
up += temp;
b[i] -= temp;
a[v.back()] -= temp;
}
v.push_back(i);
}
for (int i = 0; i < n; i++) {
// try to get as many neutral votes as possible
int temp = min(a[i], b[i]);
a[i] -= temp;
b[i] -= temp;
// the people left will downvote
down += b[i];
}
cout << up-down << '\n';
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... |