#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ar array
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 2e5 + 5, inf = 1e9;
class fenwick_tree
{
public:
vector<ll> cnt; int n;
void upd(int i, int v)
{
for(; i <= n; i += i & -i)
cnt[i] += v;
}
void upd(int l, int r, int v)
{
upd(l, v); upd(r + 1, -v);
}
ll sum(int i)
{
ll res = 0;
for(; i; i -= i & -i)
res += cnt[i];
return res;
}
void init(int _n)
{
n = _n;
cnt.assign(n + 5, 0);
}
}ft;
int le[maxn], ri[maxn], par[maxn], sz[maxn];
int N, M;
ll f[maxn];
int a[maxn];
vector<ar<int, 2>> vec[maxn];
vector<int> same[maxn];
ll res = 0;
void init(void)
{
ft.init(N);
fill(sz + 1, sz + 1 + N, 1);
iota(le + 1, le + 1 + N, 1);
iota(ri + 1, ri + 1 + N, 1);
iota(par + 1, par + 1 + N, 1);
}
int finds(int u)
{
if(par[u] == u) return u;
return par[u] = finds(par[u]);
}
void merges(int u, int v)
{
if((u = finds(u)) == (v = finds(v)))
return;
if(sz[u] < sz[v]) swap(u, v);
ft.upd(le[v], ri[v], f[u]);
ft.upd(le[u], ri[u], f[v]);
f[u] += f[v];
sz[u] += sz[v];
par[v] = u;
le[u] = min(le[u], le[v]);
ri[u] = max(ri[u], ri[v]);
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
#endif // LOCAL
cin >> N; init();
for(int i = 1; i <= N; ++i){
cin >> a[i];
same[a[i]].eb(i);
}
cin >> M;
while(M--){
int x, y, z; cin >> x >> y >> z;
vec[y - 1].pb({x, z});
res += z;
}
for(int y = 0; y <= N; ++y){
for(int i : same[y]){
if(i - 1 >= 1 && a[i - 1] <= a[i])
merges(i, i - 1);
if(i + 1 <= N && a[i + 1] <= a[i])
merges(i, i + 1);
}
for(auto v : vec[y]){
f[finds(v[0])] = max(f[finds(v[0])], v[1] + ft.sum(v[0]));
}
}
cout << res - f[finds(1)];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Incorrect |
10 ms |
9856 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Incorrect |
10 ms |
9856 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Incorrect |
10 ms |
9856 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |