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>
#define all(x) (x).begin(),(x).end()
using namespace std;
//#ifndef DLOCAL
// #define cin fin
// #define cout fout
// ifstream cin(".in");
// ofstream cout(".out");
//#endif
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 5e5 + 5;
int Ld[nmax], Rd[nmax];
int Lp[nmax], Rp[nmax];
int v[nmax];
bool possible(multiset<int> s) {
int last = -1, timer = 0;
for(auto x : s) {
if(last == -1) {
timer += x - 1;
}
else {
if(Ld[last] != Ld[x] || Rd[last] != Rd[x])
timer += x - last;
else timer += min(last - Ld[last] + x - Ld[x], Rd[last] - last + Rd[x] - x);
}
if(timer > 2 * (x - 1)) { return 0; }
last = x;
//cerr << timer << '\n';
}
//cerr << '\n';
//for(auto x: s) cerr << x << ' '; cerr << '\n';
//cerr << "ok\n";
return 1;
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int n;
cin >> n;
ll cost = 0;
for(int i = 1; i <= n; i++)
cin >> v[i], cost += (v[i] != -1? v[i] : 0);
for(int lst = -1e9 - 5, i = 1; i <= n; i++) {
if(v[i] == -1) lst = i;
else Ld[i] = lst;
}
for(int lst = 1e9 + 5, i = n; i > 0; i--) {
if(v[i] == -1) lst = i;
else Rd[i] = lst;
}
vector<vector<pii>> idxs(n + 1);
multiset<int> sofar;
auto add___ = [&](int l, int r) {
//cerr << l << ' ' << r << '\n';
vector<pii> bydist;
for(int i = l; i <= r; i++) {
if(v[i] > 0) bydist.emplace_back(min(i - Ld[i], Rd[i] - i), i);
}
sort(all(bydist));
int last = 0;
for(auto [D, p] : bydist) {
if(p - Ld[p] < Rd[p] - p) {
idxs[last].emplace_back(p, 1);
idxs[D].emplace_back(p, v[p] - 1);
last = D;
}
else {
idxs[D].emplace_back(p, v[p]);
}
}
return;
};
for(int i = 1; i <= n; i++) {
if(v[i] == -1) continue;
add___(max(0, Ld[i]) + 1, min(n + 1, Rd[i]) - 1);
i = Rd[i];
}
//cerr << cost << '\n';
//for(int i = 0; i < 10; i++) {
for(auto &T : idxs) {
for(auto &[x, c] : T) {
while(c > 0) {
sofar.emplace(x);
if(possible(sofar)) c--, cost--;
else {
sofar.erase(sofar.find(x));
break;
}
}
}
}
//}
cout << cost << '\n';
}
# | 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... |