# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1116019 | simona1230 | Magic Tree (CEOI19_magictree) | C++17 | 1919 ms | 1048576 KiB |
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>
#define endl '\n'
using namespace std;
long long n,m,k;
long long d[100001];
long long w[100001];
vector<long long> v[100001];
void read()
{
cin>>n>>m>>k;
for(long long i=2;i<=n;i++)
{
long long p;
cin>>p;
v[p].push_back(i);
}
for(long long i=1;i<=m;i++)
{
long long x;
cin>>x;
cin>>d[x]>>w[x];
}
}
set<long long> s[100001];
unordered_map<long long,long long> mp[100001];
void dfs(long long i)
{
//cout<<i<<endl;
for(long long j=0;j<v[i].size();j++)
{
long long nb=v[i][j];
dfs(nb);
for(auto it=s[nb].begin();it!=s[nb].end();it++)
{
long long c=*it;
s[i].insert(c);
mp[i][c]+=mp[nb][c];
}
}
long long rem=w[i];
while(rem&&s[i].upper_bound(d[i])!=s[i].end())
{
auto it=s[i].upper_bound(d[i]);
long long in=min(rem,mp[i][*it]);
mp[i][*it]-=in;
rem-=in;
if(mp[i][*it]==0)s[i].erase(*it);
}
if(d[i])s[i].insert(d[i]);
mp[i][d[i]]+=w[i];
/*cout<<"! "<<i<<endl;
for(auto it=s[i].begin();it!=s[i].end();it++)
cout<<*it<<" "<<mp[i][*it]<<endl;*/
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
dfs(1);
long long ans=0;
for(auto it=s[1].begin();it!=s[1].end();it++)
ans+=mp[1][*it];
cout<<ans<<endl;
return 0;
}
Compilation message (stderr)
# | 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... |