This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long N;
cin >> N;
vector<long long> Tab(N), P(N);
set<long long> M;
for (long long i = 0; i < N; i++)
{
cin >> Tab[i];
}
for (long long i = 0; i < N; i++)
{
cin >> P[i];
}
long long tot = 0;
stack<pair<long long, long long>> S;
for (long long i = N - 2; i >= 0; i--)
{
S.push({P[i + 1], i + 1});
while (Tab[i] > 0 && !S.empty())
{
long long temp = min(Tab[i], S.top().first);
Tab[i] -= temp;
tot += temp;
S.top().first -= temp;
P[S.top().second] -= temp;
if (S.top().first == 0)
S.pop();
}
}
for (long long i = 0; i < N; i++)
{
long long temp = min(Tab[i], P[i]);
Tab[i] -= temp;
P[i] -= temp;
}
for (long long i = 0; i < N; i++)
tot -= P[i];
cout << tot;
}
# | 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... |