Submission #411686

#TimeUsernameProblemLanguageResultExecution timeMemory
411686soroushFireworks (APIO16_fireworks)C++17
19 / 100
22 ms688 KiB
//叫んで 藻掻もがいて 瞼まぶたを腫らしても
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll , ll> pii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 300 + 10;
const ll mod = 1e9+7;

#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}

int n , m;
vector < pii > adj[maxn];
int dp[maxn][maxn];

int solve(int v){
	if(adj[v].size() == 0){
		for(int i = 0 ; i < maxn ; i ++)dp[v][i] = 1e6;
		dp[v][0] = 0;
		return 0;
	}
	for(auto u : adj[v])
		solve(u.first);
	for(int i = 0 ; i < maxn ; i ++){
		for(auto uw : adj[v]){
			int u = uw.first , w = uw.second;
			int mn = 1e6;
			for(int j = 0 ; j <= i ; j ++)
				mn = min(mn , abs(w - j) + dp[u][i - j]);
			dp[v][i] += mn;
		}
	}
	
	return *min_element(dp[v] , dp[v] + maxn);
}

int32_t main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin >> n >> m;
	n += m;
	for(int i = 2 ; i <= n ; i ++){
		int v , w;
		cin >> v >> w;
		adj[v].pb({i , w});
	}
	cout << solve(1);
	return(0);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...