#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
#define int long long
using namespace std;
const int mod=1e9+7,mxn=2e5+5,inf=1e18,minf=-1e18,lg=30;
//#undef int
int n,k,m;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
int h[mxn+10];
vector<pair<pii,int>>star;
vector<pii>add[mxn+10];
map<int,int>dp[mxn+10],dp2[mxn+10];
int last[mxn+10],sub[mxn+10];
int l[mxn+10],r[mxn+10];
void shift(int cur,int x){
auto it=dp2[cur].begin();
while(it!=dp2[cur].end()&&(*it).f<=x){
if((*it).s-last[cur]>0){
dp[cur][(*it).f]+=(*it).s-last[cur];
sub[cur]+=(*it).s-last[cur];
last[cur]=(*it).s;
}
it=dp2[cur].erase(it);
}
}
pii a={-1,-1},b={-1,-1};
void merge(int p,int c){
shift(c,h[p]);
if(dp[p].size()<dp[c].size())swap(dp[p],dp[c]);
if(dp2[p].size()<dp2[c].size())swap(dp2[p],dp2[c]),swap(sub[p],sub[c]),swap(last[p],last[c]);
for(auto i:dp[c])dp[p][i.f]+=i.s;
for(auto i:dp2[c])dp2[p][i.f]=max(dp2[p][i.f],i.s-sub[c]+sub[p]);
}
void dfs(int cur){
for(auto i:add[cur])dp2[cur][i.f]=i.s;
if(l[cur])dfs(l[cur]),merge(cur,l[cur]);
if(r[cur])dfs(r[cur]),merge(cur,r[cur]);
}
int32_t main(){
fastio
int ans=0;
cin>>n;
for(int i=1;i<=n;i++)cin>>h[i];
cin>>m;
for(int i=0;i<m;i++){
int x,y,c;cin>>x>>y>>c;
ans+=c;
add[x].pb({y,c});
}
stack<int>st;
int root=0;
for(int i=1;i<=n;i++){
if(h[i]>=h[root])root=i;
while(!st.empty()&&h[st.top()]<=h[i])l[i]=st.top(),st.pop();
if(!st.empty())r[st.top()]=i;
st.push(i);
}
dfs(root);
for(auto i:dp[root])ans-=i.s;
int mx=0;
for(auto i:dp2[root])mx=max(mx,i.s-sub[root]);
cout<<ans-mx;
}
/*
7 9 0 8HERE
2 9
_____
3 18
4 22
5 3
5 0 0 0HERE
_____
4 8
5 3
8 17
6 8 5 7HERE
2 9
3 18
4 12
_____
5 3
6 15
8 17
*/
Compilation message
constellation3.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
32 | #pragma GCC optimize ("03,unroll-lopps")
| ^
constellation3.cpp:38:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
38 | void setIO(string name){
| ^
constellation3.cpp:49:25: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
49 | void shift(int cur,int x){
| ^
constellation3.cpp:61:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
61 | void merge(int p,int c){
| ^
constellation3.cpp:68:17: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
68 | void dfs(int cur){
| ^
constellation3.cpp:73:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
73 | int32_t main(){
| ^
constellation3.cpp: In function 'void setIO(std::string)':
constellation3.cpp:40:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
constellation3.cpp:41:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
31056 KB |
Output is correct |
2 |
Correct |
5 ms |
29144 KB |
Output is correct |
3 |
Correct |
6 ms |
29008 KB |
Output is correct |
4 |
Correct |
5 ms |
31056 KB |
Output is correct |
5 |
Correct |
6 ms |
29460 KB |
Output is correct |
6 |
Correct |
5 ms |
29008 KB |
Output is correct |
7 |
Correct |
5 ms |
31244 KB |
Output is correct |
8 |
Correct |
5 ms |
29264 KB |
Output is correct |
9 |
Correct |
5 ms |
29008 KB |
Output is correct |
10 |
Correct |
6 ms |
31192 KB |
Output is correct |
11 |
Correct |
5 ms |
31224 KB |
Output is correct |
12 |
Correct |
5 ms |
31248 KB |
Output is correct |
13 |
Correct |
4 ms |
29264 KB |
Output is correct |
14 |
Correct |
5 ms |
31056 KB |
Output is correct |
15 |
Correct |
4 ms |
29140 KB |
Output is correct |
16 |
Correct |
5 ms |
31312 KB |
Output is correct |
17 |
Correct |
5 ms |
29148 KB |
Output is correct |
18 |
Correct |
4 ms |
29264 KB |
Output is correct |
19 |
Correct |
4 ms |
29008 KB |
Output is correct |
20 |
Correct |
5 ms |
29008 KB |
Output is correct |
21 |
Correct |
4 ms |
29264 KB |
Output is correct |
22 |
Correct |
4 ms |
29008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
31056 KB |
Output is correct |
2 |
Correct |
5 ms |
29144 KB |
Output is correct |
3 |
Correct |
6 ms |
29008 KB |
Output is correct |
4 |
Correct |
5 ms |
31056 KB |
Output is correct |
5 |
Correct |
6 ms |
29460 KB |
Output is correct |
6 |
Correct |
5 ms |
29008 KB |
Output is correct |
7 |
Correct |
5 ms |
31244 KB |
Output is correct |
8 |
Correct |
5 ms |
29264 KB |
Output is correct |
9 |
Correct |
5 ms |
29008 KB |
Output is correct |
10 |
Correct |
6 ms |
31192 KB |
Output is correct |
11 |
Correct |
5 ms |
31224 KB |
Output is correct |
12 |
Correct |
5 ms |
31248 KB |
Output is correct |
13 |
Correct |
4 ms |
29264 KB |
Output is correct |
14 |
Correct |
5 ms |
31056 KB |
Output is correct |
15 |
Correct |
4 ms |
29140 KB |
Output is correct |
16 |
Correct |
5 ms |
31312 KB |
Output is correct |
17 |
Correct |
5 ms |
29148 KB |
Output is correct |
18 |
Correct |
4 ms |
29264 KB |
Output is correct |
19 |
Correct |
4 ms |
29008 KB |
Output is correct |
20 |
Correct |
5 ms |
29008 KB |
Output is correct |
21 |
Correct |
4 ms |
29264 KB |
Output is correct |
22 |
Correct |
4 ms |
29008 KB |
Output is correct |
23 |
Correct |
7 ms |
31568 KB |
Output is correct |
24 |
Correct |
6 ms |
29520 KB |
Output is correct |
25 |
Correct |
6 ms |
31612 KB |
Output is correct |
26 |
Correct |
6 ms |
29688 KB |
Output is correct |
27 |
Correct |
6 ms |
29724 KB |
Output is correct |
28 |
Correct |
6 ms |
29520 KB |
Output is correct |
29 |
Correct |
6 ms |
29388 KB |
Output is correct |
30 |
Correct |
7 ms |
31568 KB |
Output is correct |
31 |
Correct |
6 ms |
29604 KB |
Output is correct |
32 |
Correct |
6 ms |
29688 KB |
Output is correct |
33 |
Correct |
6 ms |
29624 KB |
Output is correct |
34 |
Correct |
6 ms |
31820 KB |
Output is correct |
35 |
Correct |
6 ms |
31568 KB |
Output is correct |
36 |
Correct |
6 ms |
29688 KB |
Output is correct |
37 |
Correct |
8 ms |
29520 KB |
Output is correct |
38 |
Correct |
5 ms |
31568 KB |
Output is correct |
39 |
Correct |
5 ms |
29520 KB |
Output is correct |
40 |
Correct |
6 ms |
29688 KB |
Output is correct |
41 |
Correct |
5 ms |
29264 KB |
Output is correct |
42 |
Correct |
5 ms |
29264 KB |
Output is correct |
43 |
Correct |
6 ms |
29520 KB |
Output is correct |
44 |
Correct |
5 ms |
31312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
31056 KB |
Output is correct |
2 |
Correct |
5 ms |
29144 KB |
Output is correct |
3 |
Correct |
6 ms |
29008 KB |
Output is correct |
4 |
Correct |
5 ms |
31056 KB |
Output is correct |
5 |
Correct |
6 ms |
29460 KB |
Output is correct |
6 |
Correct |
5 ms |
29008 KB |
Output is correct |
7 |
Correct |
5 ms |
31244 KB |
Output is correct |
8 |
Correct |
5 ms |
29264 KB |
Output is correct |
9 |
Correct |
5 ms |
29008 KB |
Output is correct |
10 |
Correct |
6 ms |
31192 KB |
Output is correct |
11 |
Correct |
5 ms |
31224 KB |
Output is correct |
12 |
Correct |
5 ms |
31248 KB |
Output is correct |
13 |
Correct |
4 ms |
29264 KB |
Output is correct |
14 |
Correct |
5 ms |
31056 KB |
Output is correct |
15 |
Correct |
4 ms |
29140 KB |
Output is correct |
16 |
Correct |
5 ms |
31312 KB |
Output is correct |
17 |
Correct |
5 ms |
29148 KB |
Output is correct |
18 |
Correct |
4 ms |
29264 KB |
Output is correct |
19 |
Correct |
4 ms |
29008 KB |
Output is correct |
20 |
Correct |
5 ms |
29008 KB |
Output is correct |
21 |
Correct |
4 ms |
29264 KB |
Output is correct |
22 |
Correct |
4 ms |
29008 KB |
Output is correct |
23 |
Correct |
7 ms |
31568 KB |
Output is correct |
24 |
Correct |
6 ms |
29520 KB |
Output is correct |
25 |
Correct |
6 ms |
31612 KB |
Output is correct |
26 |
Correct |
6 ms |
29688 KB |
Output is correct |
27 |
Correct |
6 ms |
29724 KB |
Output is correct |
28 |
Correct |
6 ms |
29520 KB |
Output is correct |
29 |
Correct |
6 ms |
29388 KB |
Output is correct |
30 |
Correct |
7 ms |
31568 KB |
Output is correct |
31 |
Correct |
6 ms |
29604 KB |
Output is correct |
32 |
Correct |
6 ms |
29688 KB |
Output is correct |
33 |
Correct |
6 ms |
29624 KB |
Output is correct |
34 |
Correct |
6 ms |
31820 KB |
Output is correct |
35 |
Correct |
6 ms |
31568 KB |
Output is correct |
36 |
Correct |
6 ms |
29688 KB |
Output is correct |
37 |
Correct |
8 ms |
29520 KB |
Output is correct |
38 |
Correct |
5 ms |
31568 KB |
Output is correct |
39 |
Correct |
5 ms |
29520 KB |
Output is correct |
40 |
Correct |
6 ms |
29688 KB |
Output is correct |
41 |
Correct |
5 ms |
29264 KB |
Output is correct |
42 |
Correct |
5 ms |
29264 KB |
Output is correct |
43 |
Correct |
6 ms |
29520 KB |
Output is correct |
44 |
Correct |
5 ms |
31312 KB |
Output is correct |
45 |
Correct |
218 ms |
88524 KB |
Output is correct |
46 |
Correct |
214 ms |
87624 KB |
Output is correct |
47 |
Correct |
218 ms |
88904 KB |
Output is correct |
48 |
Correct |
215 ms |
86832 KB |
Output is correct |
49 |
Correct |
206 ms |
87560 KB |
Output is correct |
50 |
Correct |
215 ms |
87112 KB |
Output is correct |
51 |
Correct |
239 ms |
87740 KB |
Output is correct |
52 |
Correct |
210 ms |
89276 KB |
Output is correct |
53 |
Correct |
220 ms |
87112 KB |
Output is correct |
54 |
Correct |
244 ms |
74740 KB |
Output is correct |
55 |
Correct |
204 ms |
70984 KB |
Output is correct |
56 |
Correct |
200 ms |
68680 KB |
Output is correct |
57 |
Correct |
222 ms |
67912 KB |
Output is correct |
58 |
Correct |
189 ms |
74804 KB |
Output is correct |
59 |
Correct |
186 ms |
75032 KB |
Output is correct |
60 |
Correct |
134 ms |
78152 KB |
Output is correct |
61 |
Correct |
133 ms |
62536 KB |
Output is correct |
62 |
Correct |
228 ms |
78664 KB |
Output is correct |
63 |
Correct |
128 ms |
59892 KB |
Output is correct |
64 |
Correct |
148 ms |
61952 KB |
Output is correct |
65 |
Correct |
206 ms |
78408 KB |
Output is correct |
66 |
Correct |
145 ms |
61000 KB |
Output is correct |