This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |