Submission #554371

# Submission time Handle Problem Language Result Execution time Memory
554371 2022-04-28T09:33:34 Z Keshi Fireworks (APIO16_fireworks) C++17
26 / 100
8 ms 1552 KB
//In the name of God
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int maxn = 512;
const ll mod = 1e9 + 7;
const ll inf = 1e15;
 
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r+", stdin);freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()
 
ll pw(ll a, ll b){
	ll c = 1;
	while(b){
		if(b & 1) c = c * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return c;
}
 
int n, m, p[maxn], c[maxn];
vector<int> g[maxn];
ll dp[maxn][maxn];
 
void dfs(int v){
	for(int u : g[v]){
		dfs(u);
		for(int i = c[v]; i < maxn; i++){
			dp[v][i] += dp[u][i - c[v]];
		}
	}
	if(g[v].empty()){
      	assert(v > n);
		fill(dp[v], dp[v] + maxn, inf);
		dp[v][c[v]] = 0;
	}
  	if(v == 1) return;
	for(int i = 0; i < c[v]; i++){
		dp[v][i] = inf;
	}
	ll mn = inf;
	for(int i = 0; i < maxn; i++){
		dp[v][i] = min(dp[v][i], i + mn);
		mn = min(mn, dp[v][i] - i);
	}
	for(int i = 0; i < maxn; i++){
		for(int j = 0; j <= c[v]; j++){
         	if(i + j >= maxn) break;
          	dp[v][i] = min(dp[v][i], dp[v][i + j] + j);
        }
	}
	return;
}
 
void solve(){
	ll ans = inf;
	for(int i = 2; i <= n + m; i++){
		ll x = 0;
		for(int j = 2; j <= n + m; j++){
			x += abs(c[i] - c[j]);
		}
		ans = min(ans, x);
	}
	cout << ans << "\n";
	return;
}
 
int main(){
	fast_io;
 
	cin >> n >> m;
	for(int i = 2; i <= n + m; i++){
		cin >> p[i] >> c[i];
		g[p[i]].pb(i);
	}
	if(n == 1){
		solve();
		return 0;
	}
	dfs(1);
	ll ans = inf;
	for(int i = 0; i < maxn; i++){
		ans = min(ans, dp[1][i]);
	}
	cout << ans << "\n";
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 3 ms 724 KB Output is correct
6 Correct 4 ms 868 KB Output is correct
7 Correct 3 ms 860 KB Output is correct
8 Correct 6 ms 980 KB Output is correct
9 Correct 4 ms 1108 KB Output is correct
10 Correct 4 ms 1108 KB Output is correct
11 Correct 5 ms 1236 KB Output is correct
12 Correct 5 ms 1364 KB Output is correct
13 Correct 6 ms 1364 KB Output is correct
14 Correct 2 ms 1492 KB Output is correct
15 Correct 7 ms 1492 KB Output is correct
16 Correct 8 ms 1492 KB Output is correct
17 Correct 7 ms 1492 KB Output is correct
18 Correct 6 ms 1492 KB Output is correct
19 Correct 7 ms 1552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 3 ms 724 KB Output is correct
16 Correct 4 ms 868 KB Output is correct
17 Correct 3 ms 860 KB Output is correct
18 Correct 6 ms 980 KB Output is correct
19 Correct 4 ms 1108 KB Output is correct
20 Correct 4 ms 1108 KB Output is correct
21 Correct 5 ms 1236 KB Output is correct
22 Correct 5 ms 1364 KB Output is correct
23 Correct 6 ms 1364 KB Output is correct
24 Correct 2 ms 1492 KB Output is correct
25 Correct 7 ms 1492 KB Output is correct
26 Correct 8 ms 1492 KB Output is correct
27 Correct 7 ms 1492 KB Output is correct
28 Correct 6 ms 1492 KB Output is correct
29 Correct 7 ms 1552 KB Output is correct
30 Runtime error 1 ms 468 KB Execution killed with signal 11
31 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 3 ms 724 KB Output is correct
16 Correct 4 ms 868 KB Output is correct
17 Correct 3 ms 860 KB Output is correct
18 Correct 6 ms 980 KB Output is correct
19 Correct 4 ms 1108 KB Output is correct
20 Correct 4 ms 1108 KB Output is correct
21 Correct 5 ms 1236 KB Output is correct
22 Correct 5 ms 1364 KB Output is correct
23 Correct 6 ms 1364 KB Output is correct
24 Correct 2 ms 1492 KB Output is correct
25 Correct 7 ms 1492 KB Output is correct
26 Correct 8 ms 1492 KB Output is correct
27 Correct 7 ms 1492 KB Output is correct
28 Correct 6 ms 1492 KB Output is correct
29 Correct 7 ms 1552 KB Output is correct
30 Runtime error 1 ms 468 KB Execution killed with signal 11
31 Halted 0 ms 0 KB -