#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using vi = vector<int>;
using pii = pair<int, int>;
using ll = long long;
using vll = vector<ll>;
const int mx = 200'000;
int N, M;
vi occ[1 + mx];
vector<pii> stars[1 + mx];
vll bit(1 + mx);
void add(int i, ll v)
{
for(int j = i; j <= N; j += j&-j)
bit[j] += v;
}
ll prefsum(int i)
{
ll res = 0;
for(int j = i; j >= 1; j -= j&-j)
res += bit[j];
return res;
}
struct DSU
{
vi par = vi(1 + mx);
vi st = vi(1 + mx);
vi l = vi(1 + mx);
vi r = vi(1 + mx);
vll dp = vll(1 + mx);
DSU()
{
for(int i = 1; i <= N; i++)
{
par[i] = i;
st[i] = 1;
l[i] = r[i] = i;
dp[i] = 0;
}
}
int root(int u)
{
if(par[u] == u) return u;
else return (par[u] = root(par[u]));
}
void join(int u, int v)
{
u = root(u);
v = root(v);
if(u == v) return;
// cerr << "joining " << u << ' ' << v << '\n';
if(st[u] < st[v]) swap(u, v);
add(l[u], +dp[v]);
add(r[u]+1, -dp[v]);
// cerr << " : " << dp[v] << '\n';
// cerr << "adding dp " << v << " to " << l[u] << ' ' << r[u] << '\n';
add(l[v], +dp[u]);
add(r[v]+1, -dp[u]);
// cerr << " : " << dp[v] << '\n';
// cerr << "adding dp " << u << " to " << l[v] << ' ' << r[v] << '\n';
par[v] = u;
st[u] += st[v];
l[u] = min(l[u], l[v]);
r[u] = max(r[u], r[v]);
dp[u] += dp[v];
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
vi A(1 + N);
for(int i = 1; i <= N; i++)
{
cin >> A[i];
occ[A[i]].push_back(i);
}
cin >> M;
ll tot = 0;
for(int j = 1; j <= M; j++)
{
int x, y, c;
cin >> x >> y >> c;
tot += c;
stars[y].push_back({x, c});
}
DSU dsu;
for(int h = 1; h <= N; h++)
{
// cerr << "\n\n\n height = " << h << '\n';
for(pii star : stars[h])
{
// cerr << "\n\n\n!!!\n";
dsu.dp[dsu.root(star.first)] = max(dsu.dp[dsu.root(star.first)], prefsum(star.first) + star.second);
// cerr << star.first << ' ' << star.second << " <> " << prefsum(star.first) << '\n';
// cerr << dsu.root(star.first) << " : " << dsu.dp[dsu.root(star.first)] << '\n';
}
for(int i : occ[h])
{
if(1 <= i-1 && A[i-1] <= h)
dsu.join(i-1, i);
if(i+1 <= N && A[i+1] <= h)
dsu.join(i, i+1);
}
}
cout << tot - dsu.dp[dsu.root(1)] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15940 KB |
Output is correct |
2 |
Correct |
7 ms |
15888 KB |
Output is correct |
3 |
Correct |
8 ms |
15936 KB |
Output is correct |
4 |
Correct |
8 ms |
15948 KB |
Output is correct |
5 |
Correct |
8 ms |
15932 KB |
Output is correct |
6 |
Correct |
8 ms |
15904 KB |
Output is correct |
7 |
Correct |
8 ms |
15908 KB |
Output is correct |
8 |
Correct |
8 ms |
15948 KB |
Output is correct |
9 |
Correct |
8 ms |
15904 KB |
Output is correct |
10 |
Correct |
8 ms |
15896 KB |
Output is correct |
11 |
Correct |
8 ms |
15904 KB |
Output is correct |
12 |
Correct |
10 ms |
15968 KB |
Output is correct |
13 |
Correct |
9 ms |
16008 KB |
Output is correct |
14 |
Correct |
8 ms |
15904 KB |
Output is correct |
15 |
Correct |
8 ms |
15904 KB |
Output is correct |
16 |
Correct |
8 ms |
15948 KB |
Output is correct |
17 |
Correct |
8 ms |
15944 KB |
Output is correct |
18 |
Correct |
9 ms |
15948 KB |
Output is correct |
19 |
Correct |
7 ms |
15948 KB |
Output is correct |
20 |
Correct |
8 ms |
15948 KB |
Output is correct |
21 |
Correct |
8 ms |
15904 KB |
Output is correct |
22 |
Correct |
8 ms |
15948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15940 KB |
Output is correct |
2 |
Correct |
7 ms |
15888 KB |
Output is correct |
3 |
Correct |
8 ms |
15936 KB |
Output is correct |
4 |
Correct |
8 ms |
15948 KB |
Output is correct |
5 |
Correct |
8 ms |
15932 KB |
Output is correct |
6 |
Correct |
8 ms |
15904 KB |
Output is correct |
7 |
Correct |
8 ms |
15908 KB |
Output is correct |
8 |
Correct |
8 ms |
15948 KB |
Output is correct |
9 |
Correct |
8 ms |
15904 KB |
Output is correct |
10 |
Correct |
8 ms |
15896 KB |
Output is correct |
11 |
Correct |
8 ms |
15904 KB |
Output is correct |
12 |
Correct |
10 ms |
15968 KB |
Output is correct |
13 |
Correct |
9 ms |
16008 KB |
Output is correct |
14 |
Correct |
8 ms |
15904 KB |
Output is correct |
15 |
Correct |
8 ms |
15904 KB |
Output is correct |
16 |
Correct |
8 ms |
15948 KB |
Output is correct |
17 |
Correct |
8 ms |
15944 KB |
Output is correct |
18 |
Correct |
9 ms |
15948 KB |
Output is correct |
19 |
Correct |
7 ms |
15948 KB |
Output is correct |
20 |
Correct |
8 ms |
15948 KB |
Output is correct |
21 |
Correct |
8 ms |
15904 KB |
Output is correct |
22 |
Correct |
8 ms |
15948 KB |
Output is correct |
23 |
Correct |
9 ms |
16076 KB |
Output is correct |
24 |
Correct |
9 ms |
16004 KB |
Output is correct |
25 |
Correct |
12 ms |
16040 KB |
Output is correct |
26 |
Correct |
9 ms |
16076 KB |
Output is correct |
27 |
Correct |
9 ms |
16076 KB |
Output is correct |
28 |
Correct |
9 ms |
16012 KB |
Output is correct |
29 |
Correct |
9 ms |
16076 KB |
Output is correct |
30 |
Correct |
9 ms |
16044 KB |
Output is correct |
31 |
Correct |
9 ms |
16036 KB |
Output is correct |
32 |
Correct |
9 ms |
16040 KB |
Output is correct |
33 |
Correct |
10 ms |
16036 KB |
Output is correct |
34 |
Correct |
10 ms |
16028 KB |
Output is correct |
35 |
Correct |
9 ms |
16044 KB |
Output is correct |
36 |
Correct |
9 ms |
16040 KB |
Output is correct |
37 |
Correct |
8 ms |
16076 KB |
Output is correct |
38 |
Correct |
9 ms |
16076 KB |
Output is correct |
39 |
Correct |
8 ms |
16076 KB |
Output is correct |
40 |
Correct |
9 ms |
16040 KB |
Output is correct |
41 |
Correct |
8 ms |
16076 KB |
Output is correct |
42 |
Correct |
9 ms |
16076 KB |
Output is correct |
43 |
Correct |
9 ms |
16112 KB |
Output is correct |
44 |
Correct |
9 ms |
16040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15940 KB |
Output is correct |
2 |
Correct |
7 ms |
15888 KB |
Output is correct |
3 |
Correct |
8 ms |
15936 KB |
Output is correct |
4 |
Correct |
8 ms |
15948 KB |
Output is correct |
5 |
Correct |
8 ms |
15932 KB |
Output is correct |
6 |
Correct |
8 ms |
15904 KB |
Output is correct |
7 |
Correct |
8 ms |
15908 KB |
Output is correct |
8 |
Correct |
8 ms |
15948 KB |
Output is correct |
9 |
Correct |
8 ms |
15904 KB |
Output is correct |
10 |
Correct |
8 ms |
15896 KB |
Output is correct |
11 |
Correct |
8 ms |
15904 KB |
Output is correct |
12 |
Correct |
10 ms |
15968 KB |
Output is correct |
13 |
Correct |
9 ms |
16008 KB |
Output is correct |
14 |
Correct |
8 ms |
15904 KB |
Output is correct |
15 |
Correct |
8 ms |
15904 KB |
Output is correct |
16 |
Correct |
8 ms |
15948 KB |
Output is correct |
17 |
Correct |
8 ms |
15944 KB |
Output is correct |
18 |
Correct |
9 ms |
15948 KB |
Output is correct |
19 |
Correct |
7 ms |
15948 KB |
Output is correct |
20 |
Correct |
8 ms |
15948 KB |
Output is correct |
21 |
Correct |
8 ms |
15904 KB |
Output is correct |
22 |
Correct |
8 ms |
15948 KB |
Output is correct |
23 |
Correct |
9 ms |
16076 KB |
Output is correct |
24 |
Correct |
9 ms |
16004 KB |
Output is correct |
25 |
Correct |
12 ms |
16040 KB |
Output is correct |
26 |
Correct |
9 ms |
16076 KB |
Output is correct |
27 |
Correct |
9 ms |
16076 KB |
Output is correct |
28 |
Correct |
9 ms |
16012 KB |
Output is correct |
29 |
Correct |
9 ms |
16076 KB |
Output is correct |
30 |
Correct |
9 ms |
16044 KB |
Output is correct |
31 |
Correct |
9 ms |
16036 KB |
Output is correct |
32 |
Correct |
9 ms |
16040 KB |
Output is correct |
33 |
Correct |
10 ms |
16036 KB |
Output is correct |
34 |
Correct |
10 ms |
16028 KB |
Output is correct |
35 |
Correct |
9 ms |
16044 KB |
Output is correct |
36 |
Correct |
9 ms |
16040 KB |
Output is correct |
37 |
Correct |
8 ms |
16076 KB |
Output is correct |
38 |
Correct |
9 ms |
16076 KB |
Output is correct |
39 |
Correct |
8 ms |
16076 KB |
Output is correct |
40 |
Correct |
9 ms |
16040 KB |
Output is correct |
41 |
Correct |
8 ms |
16076 KB |
Output is correct |
42 |
Correct |
9 ms |
16076 KB |
Output is correct |
43 |
Correct |
9 ms |
16112 KB |
Output is correct |
44 |
Correct |
9 ms |
16040 KB |
Output is correct |
45 |
Correct |
261 ms |
29932 KB |
Output is correct |
46 |
Correct |
187 ms |
29824 KB |
Output is correct |
47 |
Correct |
200 ms |
30016 KB |
Output is correct |
48 |
Correct |
186 ms |
29764 KB |
Output is correct |
49 |
Correct |
210 ms |
29880 KB |
Output is correct |
50 |
Correct |
183 ms |
29552 KB |
Output is correct |
51 |
Correct |
224 ms |
29688 KB |
Output is correct |
52 |
Correct |
188 ms |
30092 KB |
Output is correct |
53 |
Correct |
200 ms |
29828 KB |
Output is correct |
54 |
Correct |
154 ms |
32272 KB |
Output is correct |
55 |
Correct |
137 ms |
32104 KB |
Output is correct |
56 |
Correct |
136 ms |
31776 KB |
Output is correct |
57 |
Correct |
160 ms |
31980 KB |
Output is correct |
58 |
Correct |
124 ms |
26608 KB |
Output is correct |
59 |
Correct |
130 ms |
26812 KB |
Output is correct |
60 |
Correct |
111 ms |
30660 KB |
Output is correct |
61 |
Correct |
97 ms |
25112 KB |
Output is correct |
62 |
Correct |
150 ms |
29508 KB |
Output is correct |
63 |
Correct |
97 ms |
24972 KB |
Output is correct |
64 |
Correct |
97 ms |
24892 KB |
Output is correct |
65 |
Correct |
138 ms |
29376 KB |
Output is correct |
66 |
Correct |
104 ms |
24856 KB |
Output is correct |