#include<bits/stdc++.h>
#include<random>
#define ll long long
#define F first
#define S second
#define in insert
#define pb push_back
#define ppb pop_back()
#define d3 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define cans cout << ans << "\n";
#define yes cout << "Yes" << "\n";
#define no cout << "No" << "\n";
#define pll pair<ll,ll>
#define lin cout << "\n";
#define sqr 340
#define mid ((l+r)/2)
#define lc (2*x)
#define rc (2*x+1)
using namespace std;
const ll N = 100009;
const ll m = 1e9+7;
ll n , q , ans;
ll p[N] , d[N];
ll path[N];
ll dp[N];
vector<ll> adj[N];
void dfs(ll x)
{
path[x]=0;
vector<ll> v;
for(auto it : adj[x])
{
dfs(it);
v.pb(path[it]+d[it]);
}
if(!v.empty())
sort(v.begin(),v.end());
if(v.size()==0)
dp[x]=0;
else if(v.size()==1)
dp[x]=v[0];
else
dp[x]=v[v.size()-1]+v[v.size()-2];
dp[x]%=m;
ans+=dp[x];
ans%=m;
if(v.size())
path[x]=v[v.size()-1];
}
int main()
{
d3
cin >> n;
p[0]=-1;
for(int i = 1 ; n>i ; i++)
{
cin >> p[i];
adj[p[i]].pb(i);
}
d[0]=0;
for(int i = 1 ; n>i ; i++)
cin >> d[i];
dfs(0);
cans
cin >> q;
while(q--)
{
ll v , ad;
cin >> v >> ad;
ans=0;
d[v]+=ad;
dfs(0);
cans
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
3084 KB |
Output is correct |
2 |
Correct |
28 ms |
2908 KB |
Output is correct |
3 |
Correct |
23 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5034 ms |
8792 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5034 ms |
12028 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
3084 KB |
Output is correct |
2 |
Correct |
28 ms |
2908 KB |
Output is correct |
3 |
Correct |
23 ms |
2908 KB |
Output is correct |
4 |
Execution timed out |
5034 ms |
8792 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |