#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 = get(solve(l, p - 1));
int y = get(solve(p + 1, r));
if(dp[x].size() > dp[y].size()) swap(x, y);
one[x] = y;
int t1 = lmao(x, a[p]);
int t2 = lmao(y, a[p]);
f[y] += t1;
// for(auto [id, w] : dp[y]) calc(y, id);
for(auto [id, w] : dp[x]){
dp[y][id] = max(calc(x, id) + t2, calc(y, id));
}
one[p] = y;
for(auto [id, w] : dp[p]){
dp[y][id] = max(calc(y, id), w + t1 + t2);
}
dp[y][a[p]] = t2 + t1;
g[y][a[p]] = f[y];
}
else{
int y;
if(l < p)y = get(solve(l, p - 1));
else y = get(solve(p + 1, r));
int t1 = lmao(y, a[p]);
one[p] = y;
for(auto [id, w] : dp[p]){
dp[y][id] = max(calc(y, id), w + t1);
}
dp[y][a[p]] = t1;
g[y][a[p]] = f[y];
// cout <<endl<< p << "#\n";
// for(auto [id, w] : dp[y]) cout << id << ' ' << w << endl;
//
// for(auto [id, w] : dp[y]){
// calc(y, id);
// }
}
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;
dp[u][v] = w;
sum += w;
}
int p = get(solve(1, n));
ll ans = 0;
for(auto [id,w] : dp[p]) ans = max(ans, calc(p, id));
cout << sum - ans;
}
/*
8
6 2 1 5 2 5 2 7
4
3 8 2
7 5 3
6 7 2
2 3 4
*/
Compilation message
constellation3.cpp:96:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
96 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19292 KB |
Output is correct |
2 |
Correct |
9 ms |
19292 KB |
Output is correct |
3 |
Correct |
9 ms |
19264 KB |
Output is correct |
4 |
Correct |
9 ms |
19292 KB |
Output is correct |
5 |
Correct |
9 ms |
19312 KB |
Output is correct |
6 |
Correct |
9 ms |
19288 KB |
Output is correct |
7 |
Correct |
10 ms |
19288 KB |
Output is correct |
8 |
Correct |
9 ms |
19292 KB |
Output is correct |
9 |
Correct |
8 ms |
19288 KB |
Output is correct |
10 |
Correct |
10 ms |
19528 KB |
Output is correct |
11 |
Correct |
9 ms |
19288 KB |
Output is correct |
12 |
Correct |
11 ms |
19292 KB |
Output is correct |
13 |
Correct |
9 ms |
19264 KB |
Output is correct |
14 |
Correct |
8 ms |
19288 KB |
Output is correct |
15 |
Correct |
9 ms |
19292 KB |
Output is correct |
16 |
Correct |
9 ms |
19196 KB |
Output is correct |
17 |
Correct |
8 ms |
19288 KB |
Output is correct |
18 |
Correct |
9 ms |
19356 KB |
Output is correct |
19 |
Correct |
9 ms |
19288 KB |
Output is correct |
20 |
Correct |
10 ms |
19288 KB |
Output is correct |
21 |
Correct |
10 ms |
19288 KB |
Output is correct |
22 |
Correct |
12 ms |
19376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19292 KB |
Output is correct |
2 |
Correct |
9 ms |
19292 KB |
Output is correct |
3 |
Correct |
9 ms |
19264 KB |
Output is correct |
4 |
Correct |
9 ms |
19292 KB |
Output is correct |
5 |
Correct |
9 ms |
19312 KB |
Output is correct |
6 |
Correct |
9 ms |
19288 KB |
Output is correct |
7 |
Correct |
10 ms |
19288 KB |
Output is correct |
8 |
Correct |
9 ms |
19292 KB |
Output is correct |
9 |
Correct |
8 ms |
19288 KB |
Output is correct |
10 |
Correct |
10 ms |
19528 KB |
Output is correct |
11 |
Correct |
9 ms |
19288 KB |
Output is correct |
12 |
Correct |
11 ms |
19292 KB |
Output is correct |
13 |
Correct |
9 ms |
19264 KB |
Output is correct |
14 |
Correct |
8 ms |
19288 KB |
Output is correct |
15 |
Correct |
9 ms |
19292 KB |
Output is correct |
16 |
Correct |
9 ms |
19196 KB |
Output is correct |
17 |
Correct |
8 ms |
19288 KB |
Output is correct |
18 |
Correct |
9 ms |
19356 KB |
Output is correct |
19 |
Correct |
9 ms |
19288 KB |
Output is correct |
20 |
Correct |
10 ms |
19288 KB |
Output is correct |
21 |
Correct |
10 ms |
19288 KB |
Output is correct |
22 |
Correct |
12 ms |
19376 KB |
Output is correct |
23 |
Correct |
11 ms |
19804 KB |
Output is correct |
24 |
Correct |
10 ms |
19804 KB |
Output is correct |
25 |
Correct |
11 ms |
19804 KB |
Output is correct |
26 |
Correct |
10 ms |
20060 KB |
Output is correct |
27 |
Correct |
9 ms |
19804 KB |
Output is correct |
28 |
Correct |
11 ms |
19804 KB |
Output is correct |
29 |
Correct |
10 ms |
19844 KB |
Output is correct |
30 |
Correct |
11 ms |
20060 KB |
Output is correct |
31 |
Correct |
14 ms |
19804 KB |
Output is correct |
32 |
Correct |
10 ms |
20060 KB |
Output is correct |
33 |
Correct |
10 ms |
20060 KB |
Output is correct |
34 |
Correct |
12 ms |
20060 KB |
Output is correct |
35 |
Correct |
11 ms |
20060 KB |
Output is correct |
36 |
Correct |
13 ms |
20056 KB |
Output is correct |
37 |
Correct |
13 ms |
20060 KB |
Output is correct |
38 |
Correct |
10 ms |
20060 KB |
Output is correct |
39 |
Correct |
11 ms |
19804 KB |
Output is correct |
40 |
Correct |
10 ms |
20060 KB |
Output is correct |
41 |
Correct |
11 ms |
19800 KB |
Output is correct |
42 |
Correct |
11 ms |
19780 KB |
Output is correct |
43 |
Correct |
11 ms |
20060 KB |
Output is correct |
44 |
Correct |
9 ms |
19940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19292 KB |
Output is correct |
2 |
Correct |
9 ms |
19292 KB |
Output is correct |
3 |
Correct |
9 ms |
19264 KB |
Output is correct |
4 |
Correct |
9 ms |
19292 KB |
Output is correct |
5 |
Correct |
9 ms |
19312 KB |
Output is correct |
6 |
Correct |
9 ms |
19288 KB |
Output is correct |
7 |
Correct |
10 ms |
19288 KB |
Output is correct |
8 |
Correct |
9 ms |
19292 KB |
Output is correct |
9 |
Correct |
8 ms |
19288 KB |
Output is correct |
10 |
Correct |
10 ms |
19528 KB |
Output is correct |
11 |
Correct |
9 ms |
19288 KB |
Output is correct |
12 |
Correct |
11 ms |
19292 KB |
Output is correct |
13 |
Correct |
9 ms |
19264 KB |
Output is correct |
14 |
Correct |
8 ms |
19288 KB |
Output is correct |
15 |
Correct |
9 ms |
19292 KB |
Output is correct |
16 |
Correct |
9 ms |
19196 KB |
Output is correct |
17 |
Correct |
8 ms |
19288 KB |
Output is correct |
18 |
Correct |
9 ms |
19356 KB |
Output is correct |
19 |
Correct |
9 ms |
19288 KB |
Output is correct |
20 |
Correct |
10 ms |
19288 KB |
Output is correct |
21 |
Correct |
10 ms |
19288 KB |
Output is correct |
22 |
Correct |
12 ms |
19376 KB |
Output is correct |
23 |
Correct |
11 ms |
19804 KB |
Output is correct |
24 |
Correct |
10 ms |
19804 KB |
Output is correct |
25 |
Correct |
11 ms |
19804 KB |
Output is correct |
26 |
Correct |
10 ms |
20060 KB |
Output is correct |
27 |
Correct |
9 ms |
19804 KB |
Output is correct |
28 |
Correct |
11 ms |
19804 KB |
Output is correct |
29 |
Correct |
10 ms |
19844 KB |
Output is correct |
30 |
Correct |
11 ms |
20060 KB |
Output is correct |
31 |
Correct |
14 ms |
19804 KB |
Output is correct |
32 |
Correct |
10 ms |
20060 KB |
Output is correct |
33 |
Correct |
10 ms |
20060 KB |
Output is correct |
34 |
Correct |
12 ms |
20060 KB |
Output is correct |
35 |
Correct |
11 ms |
20060 KB |
Output is correct |
36 |
Correct |
13 ms |
20056 KB |
Output is correct |
37 |
Correct |
13 ms |
20060 KB |
Output is correct |
38 |
Correct |
10 ms |
20060 KB |
Output is correct |
39 |
Correct |
11 ms |
19804 KB |
Output is correct |
40 |
Correct |
10 ms |
20060 KB |
Output is correct |
41 |
Correct |
11 ms |
19800 KB |
Output is correct |
42 |
Correct |
11 ms |
19780 KB |
Output is correct |
43 |
Correct |
11 ms |
20060 KB |
Output is correct |
44 |
Correct |
9 ms |
19940 KB |
Output is correct |
45 |
Correct |
268 ms |
99940 KB |
Output is correct |
46 |
Correct |
244 ms |
98644 KB |
Output is correct |
47 |
Correct |
266 ms |
99412 KB |
Output is correct |
48 |
Correct |
251 ms |
98992 KB |
Output is correct |
49 |
Correct |
249 ms |
97876 KB |
Output is correct |
50 |
Correct |
246 ms |
97620 KB |
Output is correct |
51 |
Correct |
252 ms |
98384 KB |
Output is correct |
52 |
Correct |
253 ms |
99920 KB |
Output is correct |
53 |
Correct |
247 ms |
98864 KB |
Output is correct |
54 |
Correct |
423 ms |
110932 KB |
Output is correct |
55 |
Correct |
407 ms |
108592 KB |
Output is correct |
56 |
Correct |
446 ms |
109160 KB |
Output is correct |
57 |
Correct |
424 ms |
109400 KB |
Output is correct |
58 |
Correct |
420 ms |
108628 KB |
Output is correct |
59 |
Correct |
406 ms |
108368 KB |
Output is correct |
60 |
Correct |
260 ms |
119124 KB |
Output is correct |
61 |
Correct |
256 ms |
98388 KB |
Output is correct |
62 |
Correct |
391 ms |
110416 KB |
Output is correct |
63 |
Correct |
244 ms |
91732 KB |
Output is correct |
64 |
Correct |
249 ms |
96124 KB |
Output is correct |
65 |
Correct |
403 ms |
110928 KB |
Output is correct |
66 |
Correct |
243 ms |
92808 KB |
Output is correct |