#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=2e5+5;
const ld pi=3.14159265359;
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
map<int,ll> f[N];
ll g[N],z[N],a[N],n,m,rt,s[N],r[N],l[N],sum,ans,t;
void work(ll x,ll y){
while(SZ(f[x])&&f[x].begin()->X<=y){
g[x]=max(g[x],f[x].begin()->Y+z[x]);f[x].erase(f[x].begin());
}
}
ll mg(ll x,ll y){
if(SZ(f[x])<SZ(f[y]))swap(x,y);
z[x]+=g[y];z[y]+=g[x];g[x]+=g[y];
for(auto o:f[y])f[x][o.X]=max(f[x][o.X],o.Y+z[y]-z[x]);
return x;
}
ll DFS(ll x){
ll y=x;
if(l[x])l[x]=DFS(l[x]),work(l[x],a[x]),y=mg(y,l[x]);
if(r[x])r[x]=DFS(r[x]),work(r[x],a[x]),y=mg(y,r[x]);
return y;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
REP1(i,n){
cin>>a[i];
ll id=t;
while(id&&a[s[id]]<a[i])--id;
if(id)r[s[id]]=i;
if(id<t)l[i]=s[id+1];
s[++id]=i,t=id;
}
rt=s[1];
cin>>m;
for(int i=1,x,y,c;i<=m;i++){
cin>>x>>y>>c;
sum+=(f[x][y]=c);
}
rt=DFS(rt),ans=g[rt];
for(auto o:f[rt])ans=max(ans,o.Y+z[rt]);
cout<<sum-ans<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9836 KB |
Output is correct |
2 |
Correct |
6 ms |
9836 KB |
Output is correct |
3 |
Correct |
7 ms |
9836 KB |
Output is correct |
4 |
Correct |
6 ms |
9836 KB |
Output is correct |
5 |
Correct |
6 ms |
9836 KB |
Output is correct |
6 |
Correct |
7 ms |
9836 KB |
Output is correct |
7 |
Correct |
6 ms |
9836 KB |
Output is correct |
8 |
Correct |
6 ms |
9836 KB |
Output is correct |
9 |
Correct |
6 ms |
9836 KB |
Output is correct |
10 |
Correct |
7 ms |
9836 KB |
Output is correct |
11 |
Correct |
7 ms |
9836 KB |
Output is correct |
12 |
Correct |
7 ms |
9836 KB |
Output is correct |
13 |
Correct |
6 ms |
9836 KB |
Output is correct |
14 |
Correct |
7 ms |
9836 KB |
Output is correct |
15 |
Correct |
6 ms |
9836 KB |
Output is correct |
16 |
Correct |
7 ms |
9836 KB |
Output is correct |
17 |
Correct |
7 ms |
9836 KB |
Output is correct |
18 |
Correct |
7 ms |
9836 KB |
Output is correct |
19 |
Correct |
7 ms |
9836 KB |
Output is correct |
20 |
Correct |
7 ms |
9836 KB |
Output is correct |
21 |
Correct |
7 ms |
9836 KB |
Output is correct |
22 |
Correct |
7 ms |
9836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9836 KB |
Output is correct |
2 |
Correct |
6 ms |
9836 KB |
Output is correct |
3 |
Correct |
7 ms |
9836 KB |
Output is correct |
4 |
Correct |
6 ms |
9836 KB |
Output is correct |
5 |
Correct |
6 ms |
9836 KB |
Output is correct |
6 |
Correct |
7 ms |
9836 KB |
Output is correct |
7 |
Correct |
6 ms |
9836 KB |
Output is correct |
8 |
Correct |
6 ms |
9836 KB |
Output is correct |
9 |
Correct |
6 ms |
9836 KB |
Output is correct |
10 |
Correct |
7 ms |
9836 KB |
Output is correct |
11 |
Correct |
7 ms |
9836 KB |
Output is correct |
12 |
Correct |
7 ms |
9836 KB |
Output is correct |
13 |
Correct |
6 ms |
9836 KB |
Output is correct |
14 |
Correct |
7 ms |
9836 KB |
Output is correct |
15 |
Correct |
6 ms |
9836 KB |
Output is correct |
16 |
Correct |
7 ms |
9836 KB |
Output is correct |
17 |
Correct |
7 ms |
9836 KB |
Output is correct |
18 |
Correct |
7 ms |
9836 KB |
Output is correct |
19 |
Correct |
7 ms |
9836 KB |
Output is correct |
20 |
Correct |
7 ms |
9836 KB |
Output is correct |
21 |
Correct |
7 ms |
9836 KB |
Output is correct |
22 |
Correct |
7 ms |
9836 KB |
Output is correct |
23 |
Correct |
9 ms |
9964 KB |
Output is correct |
24 |
Correct |
8 ms |
9964 KB |
Output is correct |
25 |
Correct |
8 ms |
9964 KB |
Output is correct |
26 |
Correct |
8 ms |
9964 KB |
Output is correct |
27 |
Correct |
8 ms |
9964 KB |
Output is correct |
28 |
Correct |
9 ms |
10120 KB |
Output is correct |
29 |
Correct |
9 ms |
10092 KB |
Output is correct |
30 |
Correct |
8 ms |
9964 KB |
Output is correct |
31 |
Correct |
8 ms |
9964 KB |
Output is correct |
32 |
Correct |
8 ms |
10092 KB |
Output is correct |
33 |
Correct |
9 ms |
10092 KB |
Output is correct |
34 |
Correct |
8 ms |
10092 KB |
Output is correct |
35 |
Correct |
8 ms |
10092 KB |
Output is correct |
36 |
Correct |
9 ms |
10092 KB |
Output is correct |
37 |
Correct |
9 ms |
10092 KB |
Output is correct |
38 |
Correct |
9 ms |
10092 KB |
Output is correct |
39 |
Correct |
10 ms |
10220 KB |
Output is correct |
40 |
Correct |
9 ms |
10092 KB |
Output is correct |
41 |
Correct |
8 ms |
10092 KB |
Output is correct |
42 |
Correct |
8 ms |
10092 KB |
Output is correct |
43 |
Correct |
8 ms |
10092 KB |
Output is correct |
44 |
Correct |
8 ms |
9984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9836 KB |
Output is correct |
2 |
Correct |
6 ms |
9836 KB |
Output is correct |
3 |
Correct |
7 ms |
9836 KB |
Output is correct |
4 |
Correct |
6 ms |
9836 KB |
Output is correct |
5 |
Correct |
6 ms |
9836 KB |
Output is correct |
6 |
Correct |
7 ms |
9836 KB |
Output is correct |
7 |
Correct |
6 ms |
9836 KB |
Output is correct |
8 |
Correct |
6 ms |
9836 KB |
Output is correct |
9 |
Correct |
6 ms |
9836 KB |
Output is correct |
10 |
Correct |
7 ms |
9836 KB |
Output is correct |
11 |
Correct |
7 ms |
9836 KB |
Output is correct |
12 |
Correct |
7 ms |
9836 KB |
Output is correct |
13 |
Correct |
6 ms |
9836 KB |
Output is correct |
14 |
Correct |
7 ms |
9836 KB |
Output is correct |
15 |
Correct |
6 ms |
9836 KB |
Output is correct |
16 |
Correct |
7 ms |
9836 KB |
Output is correct |
17 |
Correct |
7 ms |
9836 KB |
Output is correct |
18 |
Correct |
7 ms |
9836 KB |
Output is correct |
19 |
Correct |
7 ms |
9836 KB |
Output is correct |
20 |
Correct |
7 ms |
9836 KB |
Output is correct |
21 |
Correct |
7 ms |
9836 KB |
Output is correct |
22 |
Correct |
7 ms |
9836 KB |
Output is correct |
23 |
Correct |
9 ms |
9964 KB |
Output is correct |
24 |
Correct |
8 ms |
9964 KB |
Output is correct |
25 |
Correct |
8 ms |
9964 KB |
Output is correct |
26 |
Correct |
8 ms |
9964 KB |
Output is correct |
27 |
Correct |
8 ms |
9964 KB |
Output is correct |
28 |
Correct |
9 ms |
10120 KB |
Output is correct |
29 |
Correct |
9 ms |
10092 KB |
Output is correct |
30 |
Correct |
8 ms |
9964 KB |
Output is correct |
31 |
Correct |
8 ms |
9964 KB |
Output is correct |
32 |
Correct |
8 ms |
10092 KB |
Output is correct |
33 |
Correct |
9 ms |
10092 KB |
Output is correct |
34 |
Correct |
8 ms |
10092 KB |
Output is correct |
35 |
Correct |
8 ms |
10092 KB |
Output is correct |
36 |
Correct |
9 ms |
10092 KB |
Output is correct |
37 |
Correct |
9 ms |
10092 KB |
Output is correct |
38 |
Correct |
9 ms |
10092 KB |
Output is correct |
39 |
Correct |
10 ms |
10220 KB |
Output is correct |
40 |
Correct |
9 ms |
10092 KB |
Output is correct |
41 |
Correct |
8 ms |
10092 KB |
Output is correct |
42 |
Correct |
8 ms |
10092 KB |
Output is correct |
43 |
Correct |
8 ms |
10092 KB |
Output is correct |
44 |
Correct |
8 ms |
9984 KB |
Output is correct |
45 |
Correct |
220 ms |
35456 KB |
Output is correct |
46 |
Correct |
208 ms |
34964 KB |
Output is correct |
47 |
Correct |
217 ms |
35692 KB |
Output is correct |
48 |
Correct |
209 ms |
35020 KB |
Output is correct |
49 |
Correct |
204 ms |
35052 KB |
Output is correct |
50 |
Correct |
203 ms |
34668 KB |
Output is correct |
51 |
Correct |
211 ms |
35052 KB |
Output is correct |
52 |
Correct |
212 ms |
35692 KB |
Output is correct |
53 |
Correct |
208 ms |
35052 KB |
Output is correct |
54 |
Correct |
292 ms |
43628 KB |
Output is correct |
55 |
Correct |
309 ms |
44012 KB |
Output is correct |
56 |
Correct |
306 ms |
43628 KB |
Output is correct |
57 |
Correct |
311 ms |
44136 KB |
Output is correct |
58 |
Correct |
312 ms |
45420 KB |
Output is correct |
59 |
Correct |
315 ms |
45616 KB |
Output is correct |
60 |
Correct |
116 ms |
39532 KB |
Output is correct |
61 |
Correct |
226 ms |
41556 KB |
Output is correct |
62 |
Correct |
299 ms |
44268 KB |
Output is correct |
63 |
Correct |
208 ms |
39460 KB |
Output is correct |
64 |
Correct |
220 ms |
40852 KB |
Output is correct |
65 |
Correct |
294 ms |
43756 KB |
Output is correct |
66 |
Correct |
218 ms |
40736 KB |
Output is correct |