Submission #107039

# Submission time Handle Problem Language Result Execution time Memory
107039 2019-04-21T13:45:40 Z maksim_gaponov Fireworks (APIO16_fireworks) C++14
26 / 100
47 ms 1792 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define len(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define pb push_back

const int MOD = 1e9 + 7;

int mod(int x) {
	x %= MOD;
	if (x < 0)
		x += MOD;
	return x;
}


const int MAXN = 310;
const int MAXA = 310;
const int INF = 1e18;

vector<vector<int>> g;
vector<vector<int>> dp;
vector<int> p;
vector<int> c;

void dfs(int u) {
	dp[u].resize(MAXA, INF);
	vector<int> sums(MAXA);
	for (auto v : g[u]) {
		dfs(v);
		for (int i = 0; i < MAXA; ++i) {
			sums[i] += dp[v][i];
		}
	}
	if (!len(g[u])) {
		for (int i = 0; i < MAXA; ++i) {
			dp[u][i] = abs(i - c[u]);
		}
		return;
	}
	for (int x = 0; x < MAXA; ++x) {
		for (int y = 0; y <= x; ++y) {
			if (u == 0 && x != y)
				continue;
			int new_c = x - y;
			dp[u][x] = min(dp[u][x], abs(c[u] - new_c) + sums[y]);
		}
	}
}

void run() {
	int n, m;
	cin >> n >> m;
	g.resize(n + m);
	dp.resize(n + m);
	vector<int> v;
	p.resize(n + m);
	c.resize(n + m);
	p[0] = -1;
	for (int i = 1; i < n + m; ++i) {
		cin >> p[i] >> c[i];
		--p[i];
		g[p[i]].pb(i);
		v.pb(c[i]);
	}
	if (n == 1) {
		sort(all(v));
		int X = v[len(v) / 2];
		int ans = 0;
		for (int i = 0; i < len(v); ++i) {
			ans += abs(v[i] - X);
		}
		cout << ans << '\n';
		return;
	} else {
		dfs(0);
		int ans = INF;
		for (auto val : dp[0])
			ans = min(ans, val);
		cout << ans << '\n';
		return;
	}
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	run();
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 316 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 7 ms 512 KB Output is correct
4 Correct 16 ms 888 KB Output is correct
5 Correct 12 ms 640 KB Output is correct
6 Correct 12 ms 640 KB Output is correct
7 Correct 13 ms 768 KB Output is correct
8 Correct 13 ms 768 KB Output is correct
9 Correct 14 ms 768 KB Output is correct
10 Correct 17 ms 1024 KB Output is correct
11 Correct 17 ms 896 KB Output is correct
12 Correct 25 ms 1024 KB Output is correct
13 Correct 29 ms 1016 KB Output is correct
14 Correct 45 ms 1792 KB Output is correct
15 Correct 28 ms 1152 KB Output is correct
16 Correct 27 ms 1152 KB Output is correct
17 Correct 29 ms 1116 KB Output is correct
18 Correct 24 ms 1152 KB Output is correct
19 Correct 21 ms 1152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 316 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 7 ms 512 KB Output is correct
14 Correct 16 ms 888 KB Output is correct
15 Correct 12 ms 640 KB Output is correct
16 Correct 12 ms 640 KB Output is correct
17 Correct 13 ms 768 KB Output is correct
18 Correct 13 ms 768 KB Output is correct
19 Correct 14 ms 768 KB Output is correct
20 Correct 17 ms 1024 KB Output is correct
21 Correct 17 ms 896 KB Output is correct
22 Correct 25 ms 1024 KB Output is correct
23 Correct 29 ms 1016 KB Output is correct
24 Correct 45 ms 1792 KB Output is correct
25 Correct 28 ms 1152 KB Output is correct
26 Correct 27 ms 1152 KB Output is correct
27 Correct 29 ms 1116 KB Output is correct
28 Correct 24 ms 1152 KB Output is correct
29 Correct 21 ms 1152 KB Output is correct
30 Incorrect 47 ms 1528 KB Output isn't correct
31 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 316 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 7 ms 512 KB Output is correct
14 Correct 16 ms 888 KB Output is correct
15 Correct 12 ms 640 KB Output is correct
16 Correct 12 ms 640 KB Output is correct
17 Correct 13 ms 768 KB Output is correct
18 Correct 13 ms 768 KB Output is correct
19 Correct 14 ms 768 KB Output is correct
20 Correct 17 ms 1024 KB Output is correct
21 Correct 17 ms 896 KB Output is correct
22 Correct 25 ms 1024 KB Output is correct
23 Correct 29 ms 1016 KB Output is correct
24 Correct 45 ms 1792 KB Output is correct
25 Correct 28 ms 1152 KB Output is correct
26 Correct 27 ms 1152 KB Output is correct
27 Correct 29 ms 1116 KB Output is correct
28 Correct 24 ms 1152 KB Output is correct
29 Correct 21 ms 1152 KB Output is correct
30 Incorrect 47 ms 1528 KB Output isn't correct
31 Halted 0 ms 0 KB -