結果
| 問題 |
No.1524 Upward Mobility
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2021-05-18 18:27:22 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 97 ms / 6,000 ms |
| コード長 | 1,185 bytes |
| コンパイル時間 | 1,004 ms |
| コンパイル使用メモリ | 99,756 KB |
| 最終ジャッジ日時 | 2025-01-21 13:44:20 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 31 |
ソースコード
#include <iostream>
#include <vector>
#include <atcoder/segtree>
using namespace std;
using ll = long long;
using ull = unsigned long long;
int RQop(int l,int r){ return max(l,r); }
int RQe(){ return -1; }
using RQ = atcoder::segtree<int,RQop,RQe>;
int N;
vector<vector<int>> E;
vector<int> A;
vector<ll> B;
RQ G;
vector<ll> dp;
int dfs(int p,int i=0){
int pre_i = i;
for(int e : E[p]) i = dfs(e,i) + 1;
G.set(A[p],i);
dp[A[p]] = B[p];
ll b = B[p];
while(true){
int nx = G.min_left(A[p],[pre_i](int j){ return pre_i > j; }) - 1;
if(nx == -1) break;
if(dp[nx] > b){ dp[nx] -= b; break; }
b -= dp[nx]; dp[nx] = 0;
G.set(nx,-1);
}
return i;
}
int main(){
cin >> N;
E.resize(N);
for(int i=1; i<N; i++){
int p; cin >> p; p--;
E[p].push_back(i);
}
A.resize(N);
for(int i=0; i<N; i++){ cin >> A[i]; A[i]--; }
B.resize(N);
for(int i=0; i<N; i++) cin >> B[i];
dp.assign(N,0);
G = RQ(N);
dfs(0);
ll ans = 0;
for(int i=0; i<N; i++) ans += dp[i];
cout << ans << "\n";
return 0;
}
struct ios_do_not_sync{
ios_do_not_sync(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
} ios_do_not_sync_instance;
Nachia