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>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> ii;
typedef complex<ld> cp;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<cp> vcp;
typedef vector<ld> vld;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vii> vvii;
#define fastIO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define forw(i,l,r) for( int i = (l) ; i < (r) ; i++ )
#define forb(i,r,l) for( int i = (r) ; i >= (l) ; i-- )
#define log2i(x) (32 - __builtin_clz((x)) - 1)
#define log2ll(x) (64 - __builtin_clzll((x)) - 1)
#define Pi 3.141592653589793
#define sz(x) (int)x.size()
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const int N = 1e5+7;
int n,m,k;
int p[N],pos[N];
ii a[N];
map<int,ll> q[N];
int add(int x, int y) {
if(sz(q[x])<sz(q[y])) swap(x,y);
for(auto tmp:q[y]) q[x][tmp.fi]+=tmp.se;
return x;
}
int main() {
fastIO;
#ifndef ONLINE_JUDGE
//freopen("test.inp","r",stdin);
//freopen("test.out","w",stdout);
#endif // ONLINE_JUDGE
cin >> n >> m >> k;
forw(i,1,n) cin >> p[i], p[i]--;
forw(i,0,m) {
int u,d,w; cin >> u >> d >> w;
u--;
a[u]=mp(d,w);
}
iota(pos,pos+n,0);
forb(i,n-1,1) {
q[pos[i]][a[i].fi]+=a[i].se;
int tmp=a[i].se;
while(true) {
auto it=q[pos[i]].upper_bound(a[i].fi);
if(it==q[pos[i]].end()) break;
if(tmp>=it->se) {
tmp-=it->se;
q[pos[i]].erase(it);
} else {it->se-=tmp; break;}
}
pos[p[i]]=add(pos[p[i]],pos[i]);
}
ll ans=0;
for(auto x:q[pos[0]]) ans+=x.se;
cout << ans;
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |