結果

問題 No.1524 Upward Mobility
ユーザー 沙耶花沙耶花
提出日時 2021-05-05 13:39:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,821 bytes
コンパイル時間 4,989 ms
コンパイル使用メモリ 264,016 KB
実行使用メモリ 624,544 KB
最終ジャッジ日時 2024-04-17 07:12:09
合計ジャッジ時間 14,270 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 273 ms
37,028 KB
testcase_01 AC 283 ms
37,156 KB
testcase_02 AC 298 ms
37,028 KB
testcase_03 MLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000

int N;
vector<vector<int>> E;
vector<int> a;
vector<long long> b;
vector<vector<int>> ps;

vector<vector<long long>> v;

void dfs(int cur){
	if(E[cur].size()==0){
		ps[cur] = {a[cur],N};
		v[cur] = {b[cur],0};
		return;
	}
	
	vector<int> pp(1,a[cur]);
	
	rep(i,E[cur].size()){
		int to = E[cur][i];
		dfs(to);
		rep(j,ps[to].size())pp.push_back(ps[to][j]);
	}
	sort(pp.begin(),pp.end());
	pp.erase(unique(pp.begin(),pp.end()),pp.end());
	
	v[cur].resize(pp.size());
	
	rep(i,E[cur].size()){
		int to = E[cur][i];
		rep(j,ps[to].size()){
			int d0 = -1;
			if(j!=0)d0 = distance(pp.begin(),lower_bound(pp.begin(),pp.end(),ps[to][j-1]));
			int d1 = distance(pp.begin(),lower_bound(pp.begin(),pp.end(),ps[to][j]));
			v[cur][d1] += v[to][j];
			if(d0!=-1)v[cur][d0] -= v[to][j];
		}
	}
	
	for(int i=v[cur].size()-1;i>=1;i--)v[cur][i-1] += v[cur][i];
	for(int i=v[cur].size()-1;i>=1;i--)v[cur][i-1] = max(v[cur][i-1],v[cur][i]);
	
	int d = distance(pp.begin(),lower_bound(pp.begin(),pp.end(),a[cur]));
	v[cur][d] += b[cur];
	for(int i=v[cur].size()-1;i>=1;i--)v[cur][i-1] = max(v[cur][i-1],v[cur][i]);
		
	
	rep(i,E[cur].size()){
		int to = E[cur][i];
		ps[to].clear();
		v[to].clear();
	}
	
	ps[cur] = pp;
	/*
	cout<<cur<<endl;
	rep(i,ps[cur].size()){
		cout<<ps[cur][i]<<','<<v[cur][i]<<endl;
	}
	*/
	
}

int main(){
	
	cin>>N;
	
	E.resize(N);
	
	rep(i,N-1){
		int p;
		scanf("%d",&p);
		p--;
		E[p].push_back(i+1);
	}
	
	a.resize(N);
	b.resize(N);
	
	rep(i,N){
		scanf("%d",&a[i]);
		a[i]--;
	}
	rep(i,N)scanf("%lld",&b[i]);
	
	ps.resize(N);
	v.resize(N);
	dfs(0);
	
	cout<<v[0][0]<<endl;
	
    return 0;
}
0