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;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef vector<pair<int, int>> vii;
typedef vector<pair<long long, long long>> vll;
typedef vector<pair<long long, int>> vli;
typedef vector<pair<int, long long>> vil;
#define bitcnt(n) __builtin_popcountll((n))
#define BIT(n, i) (((n) >> (i)) & 1ll)
#define FLIP(n, i) ((n) ^ (1ll << (i)))
#define MASK(i) (1ll << (i))
#define ON(n, i) ((n) | MASK(i))
#define OFF(n, i) ((n) & ~MASK(i))
#define Int __int128
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) a / gcd(a, b) * b
#define fi first
#define se second
template <class T1, class T2>
bool maximize(T1 &x, T2 y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <class T1, class T2>
bool minimize(T1 &x, T2 y) {
if (x > y) {
x = y;
return true;
}
return false;
}
const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
int n;
int a[N], b[N];
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i] >> b[i];
}
ll ans = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] >= b[i]) continue;
else {
for (int l = i - 1, r = i + 1; a[i] < b[i] && (l > 0 || r <= n); --l, ++r) {
if (l > 0) {
int take = min(a[l] - b[l], b[i] - a[i]);
ans += 1ll * take * (i - l); a[l] -= take, a[i] += take;
}
if (a[i] >= b[i]) break;
if (r <= n) {
int take = min(a[r], b[i] - a[i]);
ans += 1ll * take * (r - i); a[r] -= take, a[i] += take;
}
}
}
}
cout << ans;
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... |