#include <bits/stdc++.h>
#define ll long long
#define ar array
#define db double
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rint(l, r) uniform_int_distribution<int>(l, r)(rng)
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
const int mod = 1e6 + 3;
void test_case() {
int n; cin >> n;
vector<int> h(n);
int mx = 0;
for (int &x : h) cin >> x, ckmax(mx, x);
vector<int> f(h);
{
int l = -1, r = -1;
for (int i = 0; i < n; i++) if (h[i] == mx) {
if (!~l) l = i;
r = i;
}
for (int i = l; i <= r; i++) f[i] = mx;
for (int i = 1; i < l; i++) ckmax(f[i], f[i-1]);
for (int i = n-2; i > r; i--) ckmax(f[i], f[i+1]);
}
int ans = 0;
vector<int> idx(n);
iota(all(idx), 0);
sort(all(idx), [&](int x, int y) { return h[x] < h[y]; });
for (int _i = 0; _i < n; _i++) {
int i = idx[_i];
if (i == 0 || i == n-1 || h[i] == f[i]) continue;
auto prog = [](int l, int r) {
return (ll)(l+r)*(r-l+1)/2;
};
auto cost = [prog](int l, int r, int extra) {
return (ll)extra * (r-l) + prog(l, r-1);
};
vector<int> l(h.begin(), h.begin() + i), r(h.begin() + i + 1, h.end());
sort(all(l));
sort(all(r));
int a = 0, b = 0;
while (h[i] < f[i]) {
if (l[a] <= h[i]) a++;
else if (r[b] <= h[i]) b++;
else {
int to = min({l[a], r[b], f[i]});
ans += cost(h[i], to, l[a] + r[b]) % mod;
if (ans >= mod) ans -= mod;
h[i] = to;
}
}
}
cout << ans << '\n';
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
while (t--) test_case();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |