#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define SZ(x) ll(x.size())
const ll MAXN = 2e5 + 10;
const ll LOG = 22;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; //998244353; //1e9 + 9;
ll n , m , S , cnt , A[MAXN] , sum[MAXN] , sz[MAXN] , tot[MAXN] , par[MAXN] , dp[MAXN] , L[MAXN] , R[MAXN];
vector<int> building[MAXN];
vector<pii> star[MAXN];
int Find(int v){
return (par[v] == -1 ? v : par[v] = Find(par[v]));
}
void merge(int v , int u){
v = Find(v) , u = Find(u);
if(u == v) return;
if(sz[v] < sz[u]) swap(v , u);
sz[v] += sz[u];
par[u] = v;
sum[v] += tot[u];
for(int i = L[u] ; i <= R[u] ; i++){
dp[i] += sum[u] + tot[v] - sum[v];
}
tot[v] += tot[u];
L[v] = min(L[v] , L[u]);
R[v] = max(R[v] , R[u]);
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
fill(par , par + MAXN , -1);
fill(sz , sz + MAXN , 1);
cin >> n;
for(int i = 1 ; i <= n ; i++){
cin >> A[i];
building[A[i] + 1].push_back(i);
L[i] = R[i] = i;
dp[i] = 0;
}
cin >> m;
for(int i = 1 ; i <= m ; i++){
int x , y , c;
cin >> x >> y >> c;
star[y].push_back({x , c});
S += c;
}
for(int i = 1 ; i <= n + 1 ; i++){
for(int j : building[i]){
if(j > 1 && A[j - 1] < i) merge(j - 1 , j);
if(j < n && A[j + 1] < i) merge(j , j + 1);
}
for(pii j : star[i]){
int pos = j.X , val = j.Y , comp = Find(pos);
tot[comp] = max(tot[comp] , dp[pos] + sum[comp] + val);
}
}
cout << S - tot[Find(1)] << endl;
return 0;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12884 KB |
Output is correct |
2 |
Correct |
9 ms |
12924 KB |
Output is correct |
3 |
Correct |
9 ms |
12876 KB |
Output is correct |
4 |
Correct |
8 ms |
12864 KB |
Output is correct |
5 |
Correct |
10 ms |
12908 KB |
Output is correct |
6 |
Correct |
8 ms |
12884 KB |
Output is correct |
7 |
Correct |
10 ms |
12884 KB |
Output is correct |
8 |
Correct |
7 ms |
12916 KB |
Output is correct |
9 |
Correct |
9 ms |
12884 KB |
Output is correct |
10 |
Correct |
9 ms |
12872 KB |
Output is correct |
11 |
Correct |
7 ms |
12880 KB |
Output is correct |
12 |
Correct |
8 ms |
12860 KB |
Output is correct |
13 |
Correct |
7 ms |
12872 KB |
Output is correct |
14 |
Correct |
7 ms |
12892 KB |
Output is correct |
15 |
Correct |
7 ms |
12884 KB |
Output is correct |
16 |
Correct |
6 ms |
12852 KB |
Output is correct |
17 |
Correct |
7 ms |
12884 KB |
Output is correct |
18 |
Correct |
7 ms |
12832 KB |
Output is correct |
19 |
Correct |
8 ms |
12884 KB |
Output is correct |
20 |
Correct |
9 ms |
12884 KB |
Output is correct |
21 |
Correct |
8 ms |
12884 KB |
Output is correct |
22 |
Correct |
7 ms |
12884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12884 KB |
Output is correct |
2 |
Correct |
9 ms |
12924 KB |
Output is correct |
3 |
Correct |
9 ms |
12876 KB |
Output is correct |
4 |
Correct |
8 ms |
12864 KB |
Output is correct |
5 |
Correct |
10 ms |
12908 KB |
Output is correct |
6 |
Correct |
8 ms |
12884 KB |
Output is correct |
7 |
Correct |
10 ms |
12884 KB |
Output is correct |
8 |
Correct |
7 ms |
12916 KB |
Output is correct |
9 |
Correct |
9 ms |
12884 KB |
Output is correct |
10 |
Correct |
9 ms |
12872 KB |
Output is correct |
11 |
Correct |
7 ms |
12880 KB |
Output is correct |
12 |
Correct |
8 ms |
12860 KB |
Output is correct |
13 |
Correct |
7 ms |
12872 KB |
Output is correct |
14 |
Correct |
7 ms |
12892 KB |
Output is correct |
15 |
Correct |
7 ms |
12884 KB |
Output is correct |
16 |
Correct |
6 ms |
12852 KB |
Output is correct |
17 |
Correct |
7 ms |
12884 KB |
Output is correct |
18 |
Correct |
7 ms |
12832 KB |
Output is correct |
19 |
Correct |
8 ms |
12884 KB |
Output is correct |
20 |
Correct |
9 ms |
12884 KB |
Output is correct |
21 |
Correct |
8 ms |
12884 KB |
Output is correct |
22 |
Correct |
7 ms |
12884 KB |
Output is correct |
23 |
Correct |
7 ms |
13012 KB |
Output is correct |
24 |
Correct |
9 ms |
13012 KB |
Output is correct |
25 |
Correct |
10 ms |
12988 KB |
Output is correct |
26 |
Correct |
9 ms |
13012 KB |
Output is correct |
27 |
Correct |
11 ms |
12984 KB |
Output is correct |
28 |
Correct |
11 ms |
13012 KB |
Output is correct |
29 |
Correct |
9 ms |
13044 KB |
Output is correct |
30 |
Correct |
9 ms |
13012 KB |
Output is correct |
31 |
Correct |
10 ms |
13012 KB |
Output is correct |
32 |
Correct |
8 ms |
13012 KB |
Output is correct |
33 |
Correct |
8 ms |
13012 KB |
Output is correct |
34 |
Correct |
10 ms |
13008 KB |
Output is correct |
35 |
Correct |
11 ms |
13052 KB |
Output is correct |
36 |
Correct |
8 ms |
13072 KB |
Output is correct |
37 |
Correct |
8 ms |
13012 KB |
Output is correct |
38 |
Correct |
11 ms |
13016 KB |
Output is correct |
39 |
Correct |
8 ms |
13012 KB |
Output is correct |
40 |
Correct |
11 ms |
13020 KB |
Output is correct |
41 |
Correct |
10 ms |
13012 KB |
Output is correct |
42 |
Correct |
11 ms |
12976 KB |
Output is correct |
43 |
Correct |
10 ms |
12992 KB |
Output is correct |
44 |
Correct |
7 ms |
13008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12884 KB |
Output is correct |
2 |
Correct |
9 ms |
12924 KB |
Output is correct |
3 |
Correct |
9 ms |
12876 KB |
Output is correct |
4 |
Correct |
8 ms |
12864 KB |
Output is correct |
5 |
Correct |
10 ms |
12908 KB |
Output is correct |
6 |
Correct |
8 ms |
12884 KB |
Output is correct |
7 |
Correct |
10 ms |
12884 KB |
Output is correct |
8 |
Correct |
7 ms |
12916 KB |
Output is correct |
9 |
Correct |
9 ms |
12884 KB |
Output is correct |
10 |
Correct |
9 ms |
12872 KB |
Output is correct |
11 |
Correct |
7 ms |
12880 KB |
Output is correct |
12 |
Correct |
8 ms |
12860 KB |
Output is correct |
13 |
Correct |
7 ms |
12872 KB |
Output is correct |
14 |
Correct |
7 ms |
12892 KB |
Output is correct |
15 |
Correct |
7 ms |
12884 KB |
Output is correct |
16 |
Correct |
6 ms |
12852 KB |
Output is correct |
17 |
Correct |
7 ms |
12884 KB |
Output is correct |
18 |
Correct |
7 ms |
12832 KB |
Output is correct |
19 |
Correct |
8 ms |
12884 KB |
Output is correct |
20 |
Correct |
9 ms |
12884 KB |
Output is correct |
21 |
Correct |
8 ms |
12884 KB |
Output is correct |
22 |
Correct |
7 ms |
12884 KB |
Output is correct |
23 |
Correct |
7 ms |
13012 KB |
Output is correct |
24 |
Correct |
9 ms |
13012 KB |
Output is correct |
25 |
Correct |
10 ms |
12988 KB |
Output is correct |
26 |
Correct |
9 ms |
13012 KB |
Output is correct |
27 |
Correct |
11 ms |
12984 KB |
Output is correct |
28 |
Correct |
11 ms |
13012 KB |
Output is correct |
29 |
Correct |
9 ms |
13044 KB |
Output is correct |
30 |
Correct |
9 ms |
13012 KB |
Output is correct |
31 |
Correct |
10 ms |
13012 KB |
Output is correct |
32 |
Correct |
8 ms |
13012 KB |
Output is correct |
33 |
Correct |
8 ms |
13012 KB |
Output is correct |
34 |
Correct |
10 ms |
13008 KB |
Output is correct |
35 |
Correct |
11 ms |
13052 KB |
Output is correct |
36 |
Correct |
8 ms |
13072 KB |
Output is correct |
37 |
Correct |
8 ms |
13012 KB |
Output is correct |
38 |
Correct |
11 ms |
13016 KB |
Output is correct |
39 |
Correct |
8 ms |
13012 KB |
Output is correct |
40 |
Correct |
11 ms |
13020 KB |
Output is correct |
41 |
Correct |
10 ms |
13012 KB |
Output is correct |
42 |
Correct |
11 ms |
12976 KB |
Output is correct |
43 |
Correct |
10 ms |
12992 KB |
Output is correct |
44 |
Correct |
7 ms |
13008 KB |
Output is correct |
45 |
Correct |
212 ms |
35352 KB |
Output is correct |
46 |
Correct |
243 ms |
35120 KB |
Output is correct |
47 |
Correct |
221 ms |
35164 KB |
Output is correct |
48 |
Correct |
241 ms |
35172 KB |
Output is correct |
49 |
Correct |
227 ms |
34840 KB |
Output is correct |
50 |
Correct |
240 ms |
34732 KB |
Output is correct |
51 |
Correct |
250 ms |
34844 KB |
Output is correct |
52 |
Correct |
221 ms |
35416 KB |
Output is correct |
53 |
Correct |
234 ms |
35084 KB |
Output is correct |
54 |
Correct |
162 ms |
36040 KB |
Output is correct |
55 |
Correct |
138 ms |
37404 KB |
Output is correct |
56 |
Correct |
159 ms |
37032 KB |
Output is correct |
57 |
Correct |
147 ms |
36968 KB |
Output is correct |
58 |
Correct |
131 ms |
31836 KB |
Output is correct |
59 |
Correct |
155 ms |
31944 KB |
Output is correct |
60 |
Correct |
110 ms |
32968 KB |
Output is correct |
61 |
Correct |
107 ms |
30588 KB |
Output is correct |
62 |
Correct |
181 ms |
34584 KB |
Output is correct |
63 |
Correct |
100 ms |
30352 KB |
Output is correct |
64 |
Correct |
103 ms |
29992 KB |
Output is correct |
65 |
Correct |
140 ms |
34584 KB |
Output is correct |
66 |
Correct |
121 ms |
29924 KB |
Output is correct |