#include <bits/stdc++.h>
#define SQR(x) (1LL * ((x) * (x)))
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(s) (int)s.size()
#define prev __prev
#define next __next
#define left __left
#define right __right
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned int ui;
using namespace std;
const int mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFLL = (ll)2e18 + 7LL;
const ld PI = acos(-1);
const int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};
template<class BUI, class TRONG>
bool minimize(BUI &x, const TRONG y){
if(x > y){
x = y;
return true;
} else return false;
}
template<class BUI, class TRONG>
bool maximize(BUI &x, const TRONG y){
if(x < y){
x = y;
return true;
} else return false;
}
/* Author : Bui Nguyen Duc Trong, Luong Van Chanh High School for the gifted*/
/* Template is copied by Trong */
/** Losing in Provincial Contests **/
/** TRYING HARDER**/
/** ORZ **/
/* -----------------[ MAIN CODE GOES HERE ]----------------- */
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e5 + 10;
const int MOD = (int)1e9 + 7;
int n;
vector<int> adj[N];
ll d[N];
ll f[N], g[N];
void dfs(int u){
pll z(0, 0);
for(int v : adj[u]){
dfs(v);
if(z.fi == 0) z.fi = f[v] + d[v];
else{
if(z.se == 0){
z.se = f[v] + d[v];
if(z.fi < z.se) swap(z.fi, z.se);
}
else{
if(!maximize(z.fi, f[v] + d[v])) maximize(z.se, f[v] + d[v]);
}
}
}
f[u] = z.fi;
g[u] = z.fi + z.se;
}
int getAns(){
dfs(0);
ll ans = 0;
for(int i = 0; i < n; i++) ans = (ans + g[i]) % MOD;
return ans;
}
void solve(){
cin >> n;
for(int i = 1; i < n; i++){
int p; cin >> p;
adj[p].push_back(i);
}
for(int i = 1; i < n; i++) cin >> d[i];
cout << getAns() << '\n';
int q;
cin >> q;
while(q--){
int v, k; cin >> v >> k;
d[v] += k;
dfs(0);
cout << getAns() << '\n';
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
const bool multitest = 0;
int tt = 1; if(multitest) cin >> tt;
while( tt-- ){
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
5052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5032 ms |
7896 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5051 ms |
10372 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
5052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |