Submission #950919

#TimeUsernameProblemLanguageResultExecution timeMemory
950919daoquanglinh2007Potatoes and fertilizers (LMIO19_bulves)C++17
24 / 100
72 ms16980 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int NM = 5e5; int N, a[NM+5], b[NM+5], ans = 0; queue <int> q; signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N; for (int i = 1; i <= N; i++){ cin >> a[i] >> b[i]; if (a[i] < b[i]) q.push(i); } for (int i = 1; i <= N; i++){ if (a[i] <= b[i]) continue; while (!q.empty() && a[i] > b[i]){ int j = q.front(); if (b[j]-a[j] <= a[i]-b[i]){ ans += abs(i-j)*(b[j]-a[j]); a[i] -= b[j]-a[j]; a[j] = b[j]; q.pop(); } else{ ans += abs(i-j)*(a[i]-b[i]); a[j] += a[i]-b[i]; a[i] = b[i]; } } } cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...