Submission #792879

# Submission time Handle Problem Language Result Execution time Memory
792879 2023-07-25T10:15:15 Z IvanJ Fireworks (APIO16_fireworks) C++17
19 / 100
29 ms 724 KB
#include<bits/stdc++.h>

#define pb push_back
#define x first
#define y second
#define all(a) (a).begin(), (a).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

const int maxn = 305;

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

int rek(int x, int len) {
	if(dp[x][len] != -1)
		return dp[x][len];
	if(adj[x].size() == 0)
		return dp[x][len] = (len == 0) ? 0 : 1e9;
	
	int ret = 0;
	for(auto p : adj[x]) {
		int reti = 1e9;
		for(int i = 0;i <= len;i++) {
			int l = len - i;
			reti = min(reti, rek(p.x, i) + abs(p.y - l));
		}
		ret += reti;
	}
	return dp[x][len] = ret;
}

int main() {
	scanf("%d%d", &n, &m);
	for(int i = 1;i < n + m;i++) {
		int p, c;
		scanf("%d%d", &p, &c);
		p--;
		adj[p].pb({i, c});
	}
	if(n == 1) {
		vector<int> v;
		for(auto p : adj[0]) v.pb(p.y);
		sort(all(v));
		int med = v[m / 2];
		ll ans = 0;
		for(int i : v) ans += (ll)(i - med);
		printf("%lld\n", ans);
		return 0;
	}
	
	memset(dp, -1, sizeof dp);
	int ans = 1e9;
	for(int i = 0;i < maxn;i++) 
		ans = min(ans, rek(0, i));
	printf("%d\n", ans);
	return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:38:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:41:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |   scanf("%d%d", &p, &c);
      |   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 2 ms 672 KB Output is correct
3 Correct 4 ms 672 KB Output is correct
4 Correct 7 ms 680 KB Output is correct
5 Correct 7 ms 596 KB Output is correct
6 Correct 11 ms 676 KB Output is correct
7 Correct 9 ms 596 KB Output is correct
8 Correct 10 ms 596 KB Output is correct
9 Correct 13 ms 680 KB Output is correct
10 Correct 12 ms 676 KB Output is correct
11 Correct 13 ms 672 KB Output is correct
12 Correct 15 ms 724 KB Output is correct
13 Correct 15 ms 680 KB Output is correct
14 Correct 15 ms 708 KB Output is correct
15 Correct 18 ms 676 KB Output is correct
16 Correct 16 ms 596 KB Output is correct
17 Correct 29 ms 672 KB Output is correct
18 Correct 17 ms 596 KB Output is correct
19 Correct 19 ms 672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -