#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, ll v)
{
for(; i <= n; i += i & -i)
cnt[i] += v;
}
void upd(int l, int r, ll 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]){
//cerr << y << ' ' << v[0] << ' ' << v[1] + ft.sum(v[0]) << '\n';
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 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
12 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
12 ms |
9856 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
10 ms |
9728 KB |
Output is correct |
15 |
Correct |
11 ms |
9856 KB |
Output is correct |
16 |
Correct |
10 ms |
9856 KB |
Output is correct |
17 |
Correct |
10 ms |
9728 KB |
Output is correct |
18 |
Correct |
12 ms |
9856 KB |
Output is correct |
19 |
Correct |
10 ms |
9856 KB |
Output is correct |
20 |
Correct |
10 ms |
9728 KB |
Output is correct |
21 |
Correct |
10 ms |
9856 KB |
Output is correct |
22 |
Correct |
10 ms |
9728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
12 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
12 ms |
9856 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
10 ms |
9728 KB |
Output is correct |
15 |
Correct |
11 ms |
9856 KB |
Output is correct |
16 |
Correct |
10 ms |
9856 KB |
Output is correct |
17 |
Correct |
10 ms |
9728 KB |
Output is correct |
18 |
Correct |
12 ms |
9856 KB |
Output is correct |
19 |
Correct |
10 ms |
9856 KB |
Output is correct |
20 |
Correct |
10 ms |
9728 KB |
Output is correct |
21 |
Correct |
10 ms |
9856 KB |
Output is correct |
22 |
Correct |
10 ms |
9728 KB |
Output is correct |
23 |
Correct |
11 ms |
9856 KB |
Output is correct |
24 |
Correct |
11 ms |
9856 KB |
Output is correct |
25 |
Correct |
11 ms |
9960 KB |
Output is correct |
26 |
Correct |
12 ms |
9856 KB |
Output is correct |
27 |
Correct |
12 ms |
9984 KB |
Output is correct |
28 |
Correct |
11 ms |
9856 KB |
Output is correct |
29 |
Correct |
11 ms |
9856 KB |
Output is correct |
30 |
Correct |
11 ms |
9856 KB |
Output is correct |
31 |
Correct |
11 ms |
9856 KB |
Output is correct |
32 |
Correct |
11 ms |
9984 KB |
Output is correct |
33 |
Correct |
11 ms |
9984 KB |
Output is correct |
34 |
Correct |
12 ms |
9984 KB |
Output is correct |
35 |
Correct |
11 ms |
9984 KB |
Output is correct |
36 |
Correct |
13 ms |
9856 KB |
Output is correct |
37 |
Correct |
12 ms |
9856 KB |
Output is correct |
38 |
Correct |
11 ms |
9984 KB |
Output is correct |
39 |
Correct |
11 ms |
9856 KB |
Output is correct |
40 |
Correct |
12 ms |
9856 KB |
Output is correct |
41 |
Correct |
11 ms |
9856 KB |
Output is correct |
42 |
Correct |
11 ms |
9856 KB |
Output is correct |
43 |
Correct |
11 ms |
9856 KB |
Output is correct |
44 |
Correct |
11 ms |
9856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
12 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
12 ms |
9856 KB |
Output is correct |
8 |
Correct |
10 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
13 |
Correct |
10 ms |
9728 KB |
Output is correct |
14 |
Correct |
10 ms |
9728 KB |
Output is correct |
15 |
Correct |
11 ms |
9856 KB |
Output is correct |
16 |
Correct |
10 ms |
9856 KB |
Output is correct |
17 |
Correct |
10 ms |
9728 KB |
Output is correct |
18 |
Correct |
12 ms |
9856 KB |
Output is correct |
19 |
Correct |
10 ms |
9856 KB |
Output is correct |
20 |
Correct |
10 ms |
9728 KB |
Output is correct |
21 |
Correct |
10 ms |
9856 KB |
Output is correct |
22 |
Correct |
10 ms |
9728 KB |
Output is correct |
23 |
Correct |
11 ms |
9856 KB |
Output is correct |
24 |
Correct |
11 ms |
9856 KB |
Output is correct |
25 |
Correct |
11 ms |
9960 KB |
Output is correct |
26 |
Correct |
12 ms |
9856 KB |
Output is correct |
27 |
Correct |
12 ms |
9984 KB |
Output is correct |
28 |
Correct |
11 ms |
9856 KB |
Output is correct |
29 |
Correct |
11 ms |
9856 KB |
Output is correct |
30 |
Correct |
11 ms |
9856 KB |
Output is correct |
31 |
Correct |
11 ms |
9856 KB |
Output is correct |
32 |
Correct |
11 ms |
9984 KB |
Output is correct |
33 |
Correct |
11 ms |
9984 KB |
Output is correct |
34 |
Correct |
12 ms |
9984 KB |
Output is correct |
35 |
Correct |
11 ms |
9984 KB |
Output is correct |
36 |
Correct |
13 ms |
9856 KB |
Output is correct |
37 |
Correct |
12 ms |
9856 KB |
Output is correct |
38 |
Correct |
11 ms |
9984 KB |
Output is correct |
39 |
Correct |
11 ms |
9856 KB |
Output is correct |
40 |
Correct |
12 ms |
9856 KB |
Output is correct |
41 |
Correct |
11 ms |
9856 KB |
Output is correct |
42 |
Correct |
11 ms |
9856 KB |
Output is correct |
43 |
Correct |
11 ms |
9856 KB |
Output is correct |
44 |
Correct |
11 ms |
9856 KB |
Output is correct |
45 |
Correct |
255 ms |
24416 KB |
Output is correct |
46 |
Correct |
242 ms |
29816 KB |
Output is correct |
47 |
Correct |
233 ms |
29816 KB |
Output is correct |
48 |
Correct |
253 ms |
30072 KB |
Output is correct |
49 |
Correct |
238 ms |
29560 KB |
Output is correct |
50 |
Correct |
237 ms |
29432 KB |
Output is correct |
51 |
Correct |
237 ms |
29560 KB |
Output is correct |
52 |
Correct |
242 ms |
30048 KB |
Output is correct |
53 |
Correct |
247 ms |
29816 KB |
Output is correct |
54 |
Correct |
190 ms |
31480 KB |
Output is correct |
55 |
Correct |
191 ms |
31992 KB |
Output is correct |
56 |
Correct |
190 ms |
31656 KB |
Output is correct |
57 |
Correct |
188 ms |
31712 KB |
Output is correct |
58 |
Correct |
167 ms |
26488 KB |
Output is correct |
59 |
Correct |
176 ms |
26616 KB |
Output is correct |
60 |
Correct |
148 ms |
29308 KB |
Output is correct |
61 |
Correct |
142 ms |
24996 KB |
Output is correct |
62 |
Correct |
190 ms |
29304 KB |
Output is correct |
63 |
Correct |
141 ms |
24548 KB |
Output is correct |
64 |
Correct |
132 ms |
24696 KB |
Output is correct |
65 |
Correct |
184 ms |
29304 KB |
Output is correct |
66 |
Correct |
144 ms |
24720 KB |
Output is correct |