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;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> h(n + 1), w(n + 1);
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
for (int i = 1; i <= n; i++) {
cin >> w[i];
w[i] += w[i - 1];
}
vector<int> s{0};
i64 sum = 0;
i64 ans = 0;
for (int i = 1; i <= n; i++) {
while (h[s.back()] > h[i]) {
int height = h[s.back()];
int r = w[s.back()];
s.pop_back();
int l = w[s.back()];
sum -= 1LL * (r - l) * (r - l + 1) / 2 * height;
sum += 1LL * (r - l) * (r - l + 1) / 2 * h[i];
}
ans += sum * (w[i] - w[i - 1]);
ans += 1LL * (w[i] - w[i - 1]) * (w[i] - w[i - 1] + 1) / 2 * h[i] * (h[i] + 1) / 2;
sum += 1LL * (w[i] - w[i - 1]) * h[i] * (h[i] + 1) / 2;
s.push_back(i);
}
cout << ans << "\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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |