#include<bits/stdc++.h>
#define ll long long
#define int long long
const int nmax = 2e5 + 5, N = 1e5;
const ll oo = 1e18 + 1, base = 311;
const int lg = 20, M = 10;
const ll mod = 998244353, mod2 = 1e9 + 5277;
#define pii pair<int, int>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
using namespace std;
int n, m;
int rmq[nmax][lg + 1], a[nmax];
map<int, int> dp[nmax], g[nmax];
ll f[nmax];
int calc(int u, int j){
// if(f[u] < g[u][j]) cout << "?";
dp[u][j] += f[u] - g[u][j];
g[u][j] = f[u];
return dp[u][j];
}
int get(int l, int r){
int k = __lg(r - l + 1);
int x = rmq[l][k], y = rmq[r - (1 << k) + 1][k];
if(a[x] > a[y])return x;
else return y;
}
int one[nmax];
int get(int u){
return one[u] ? one[u] = get(one[u]) : u;
}
int lmao(int x, int y){
ll res = 0;
while(dp[x].size()){
pii it = *dp[x].begin();
if(it.fi <= y){
res = max(res, calc(x, it.fi));
dp[x].erase(dp[x].begin());
}
else break;
}
return res;
}
int solve(int l, int r){
if(l == r) return l;
int p = get(l, r);
if(l < p && p < r){
int x = solve(l, p - 1);
int y = solve(p + 1, r);
int t1 = 0, t2 = 0;
for(int j = 1; j <= a[p]; ++j){
t1 = max(t1, dp[x][j]);
t2 = max(t2, dp[y][j]);
}
for(int j = 1; j <= n; ++j){
if(j <= a[p]){
dp[p][j] = t1 + t2;
}
else {
dp[p][j] = max({dp[x][j] + t2, dp[y][j] +t1, g[p][j] + t1 +t2});
}
}
}
else{
int y;
if(l < p) y = solve(l, p - 1);
else y = solve(p + 1, r);
int t2 = 0;
for(int j = 1; j <= a[p]; ++j){
t2 = max(t2, dp[y][j]);
}
for(int j = 1; j <= n; ++j){
if(j <= a[p]){
dp[p][j] = t2;
}
else dp[p][j] = max(dp[y][j], g[p][j] + t2);
}
}
return p;
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i], rmq[i][0] = i;
for(int j = 1; j <= lg; ++j){
for(int i = 1; i + (1 << j) - 1 <= n; ++i){
int x = rmq[i][j - 1], y = rmq[i + (1 << (j - 1))][j - 1];
if(a[x] > a[y]) rmq[i][j] = x;
else rmq[i][j] = y;
}
}
cin >> m;
ll sum = 0;
for(int i = 1, u, v, w; i <= m; ++i){
cin >> u >> v >> w;
g[u][v] = w;
dp[u][v] = w;
sum += w;
}
int p = get(solve(1, n));
ll ans = 0;
for(auto [id,w] : dp[p]) ans = max(ans, w);
cout << sum-ans;
}
/*
5 4
1 2
2 3
3 4
3 5
1 2 1 3 4
*/
Compilation message
constellation3.cpp:88:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
88 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
25924 KB |
Output is correct |
2 |
Correct |
20 ms |
25688 KB |
Output is correct |
3 |
Correct |
19 ms |
26004 KB |
Output is correct |
4 |
Correct |
18 ms |
25436 KB |
Output is correct |
5 |
Correct |
19 ms |
25044 KB |
Output is correct |
6 |
Correct |
17 ms |
24924 KB |
Output is correct |
7 |
Correct |
16 ms |
24924 KB |
Output is correct |
8 |
Correct |
22 ms |
25944 KB |
Output is correct |
9 |
Correct |
19 ms |
26076 KB |
Output is correct |
10 |
Correct |
21 ms |
26716 KB |
Output is correct |
11 |
Correct |
20 ms |
26604 KB |
Output is correct |
12 |
Correct |
20 ms |
26460 KB |
Output is correct |
13 |
Correct |
21 ms |
25532 KB |
Output is correct |
14 |
Correct |
20 ms |
25936 KB |
Output is correct |
15 |
Correct |
19 ms |
26712 KB |
Output is correct |
16 |
Correct |
22 ms |
27740 KB |
Output is correct |
17 |
Correct |
16 ms |
24204 KB |
Output is correct |
18 |
Correct |
21 ms |
25688 KB |
Output is correct |
19 |
Correct |
17 ms |
24152 KB |
Output is correct |
20 |
Correct |
20 ms |
24088 KB |
Output is correct |
21 |
Correct |
22 ms |
25692 KB |
Output is correct |
22 |
Correct |
17 ms |
24668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
25924 KB |
Output is correct |
2 |
Correct |
20 ms |
25688 KB |
Output is correct |
3 |
Correct |
19 ms |
26004 KB |
Output is correct |
4 |
Correct |
18 ms |
25436 KB |
Output is correct |
5 |
Correct |
19 ms |
25044 KB |
Output is correct |
6 |
Correct |
17 ms |
24924 KB |
Output is correct |
7 |
Correct |
16 ms |
24924 KB |
Output is correct |
8 |
Correct |
22 ms |
25944 KB |
Output is correct |
9 |
Correct |
19 ms |
26076 KB |
Output is correct |
10 |
Correct |
21 ms |
26716 KB |
Output is correct |
11 |
Correct |
20 ms |
26604 KB |
Output is correct |
12 |
Correct |
20 ms |
26460 KB |
Output is correct |
13 |
Correct |
21 ms |
25532 KB |
Output is correct |
14 |
Correct |
20 ms |
25936 KB |
Output is correct |
15 |
Correct |
19 ms |
26712 KB |
Output is correct |
16 |
Correct |
22 ms |
27740 KB |
Output is correct |
17 |
Correct |
16 ms |
24204 KB |
Output is correct |
18 |
Correct |
21 ms |
25688 KB |
Output is correct |
19 |
Correct |
17 ms |
24152 KB |
Output is correct |
20 |
Correct |
20 ms |
24088 KB |
Output is correct |
21 |
Correct |
22 ms |
25692 KB |
Output is correct |
22 |
Correct |
17 ms |
24668 KB |
Output is correct |
23 |
Correct |
653 ms |
302188 KB |
Output is correct |
24 |
Correct |
681 ms |
304464 KB |
Output is correct |
25 |
Correct |
661 ms |
303952 KB |
Output is correct |
26 |
Correct |
707 ms |
325032 KB |
Output is correct |
27 |
Correct |
669 ms |
309948 KB |
Output is correct |
28 |
Correct |
641 ms |
302908 KB |
Output is correct |
29 |
Correct |
684 ms |
314196 KB |
Output is correct |
30 |
Correct |
742 ms |
335696 KB |
Output is correct |
31 |
Correct |
648 ms |
305236 KB |
Output is correct |
32 |
Correct |
767 ms |
330352 KB |
Output is correct |
33 |
Correct |
829 ms |
353784 KB |
Output is correct |
34 |
Correct |
766 ms |
337660 KB |
Output is correct |
35 |
Correct |
800 ms |
348340 KB |
Output is correct |
36 |
Correct |
961 ms |
401196 KB |
Output is correct |
37 |
Correct |
930 ms |
404884 KB |
Output is correct |
38 |
Correct |
894 ms |
395616 KB |
Output is correct |
39 |
Correct |
652 ms |
264532 KB |
Output is correct |
40 |
Correct |
774 ms |
311380 KB |
Output is correct |
41 |
Correct |
622 ms |
261456 KB |
Output is correct |
42 |
Correct |
654 ms |
267108 KB |
Output is correct |
43 |
Correct |
691 ms |
312272 KB |
Output is correct |
44 |
Correct |
605 ms |
257360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
25924 KB |
Output is correct |
2 |
Correct |
20 ms |
25688 KB |
Output is correct |
3 |
Correct |
19 ms |
26004 KB |
Output is correct |
4 |
Correct |
18 ms |
25436 KB |
Output is correct |
5 |
Correct |
19 ms |
25044 KB |
Output is correct |
6 |
Correct |
17 ms |
24924 KB |
Output is correct |
7 |
Correct |
16 ms |
24924 KB |
Output is correct |
8 |
Correct |
22 ms |
25944 KB |
Output is correct |
9 |
Correct |
19 ms |
26076 KB |
Output is correct |
10 |
Correct |
21 ms |
26716 KB |
Output is correct |
11 |
Correct |
20 ms |
26604 KB |
Output is correct |
12 |
Correct |
20 ms |
26460 KB |
Output is correct |
13 |
Correct |
21 ms |
25532 KB |
Output is correct |
14 |
Correct |
20 ms |
25936 KB |
Output is correct |
15 |
Correct |
19 ms |
26712 KB |
Output is correct |
16 |
Correct |
22 ms |
27740 KB |
Output is correct |
17 |
Correct |
16 ms |
24204 KB |
Output is correct |
18 |
Correct |
21 ms |
25688 KB |
Output is correct |
19 |
Correct |
17 ms |
24152 KB |
Output is correct |
20 |
Correct |
20 ms |
24088 KB |
Output is correct |
21 |
Correct |
22 ms |
25692 KB |
Output is correct |
22 |
Correct |
17 ms |
24668 KB |
Output is correct |
23 |
Correct |
653 ms |
302188 KB |
Output is correct |
24 |
Correct |
681 ms |
304464 KB |
Output is correct |
25 |
Correct |
661 ms |
303952 KB |
Output is correct |
26 |
Correct |
707 ms |
325032 KB |
Output is correct |
27 |
Correct |
669 ms |
309948 KB |
Output is correct |
28 |
Correct |
641 ms |
302908 KB |
Output is correct |
29 |
Correct |
684 ms |
314196 KB |
Output is correct |
30 |
Correct |
742 ms |
335696 KB |
Output is correct |
31 |
Correct |
648 ms |
305236 KB |
Output is correct |
32 |
Correct |
767 ms |
330352 KB |
Output is correct |
33 |
Correct |
829 ms |
353784 KB |
Output is correct |
34 |
Correct |
766 ms |
337660 KB |
Output is correct |
35 |
Correct |
800 ms |
348340 KB |
Output is correct |
36 |
Correct |
961 ms |
401196 KB |
Output is correct |
37 |
Correct |
930 ms |
404884 KB |
Output is correct |
38 |
Correct |
894 ms |
395616 KB |
Output is correct |
39 |
Correct |
652 ms |
264532 KB |
Output is correct |
40 |
Correct |
774 ms |
311380 KB |
Output is correct |
41 |
Correct |
622 ms |
261456 KB |
Output is correct |
42 |
Correct |
654 ms |
267108 KB |
Output is correct |
43 |
Correct |
691 ms |
312272 KB |
Output is correct |
44 |
Correct |
605 ms |
257360 KB |
Output is correct |
45 |
Execution timed out |
1533 ms |
505732 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |