Submission #962555

# Submission time Handle Problem Language Result Execution time Memory
962555 2024-04-13T19:35:01 Z BhavayGoyal Fireworks (APIO16_fireworks) C++14
26 / 100
18 ms 1372 KB
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<class T> using oset = 
            tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"

const int MOD = 1e9+7;
const int inf = 1e9;
const int linf = 1e18;

const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};

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


// -------------------------------------------------- Main Code --------------------------------------------------

const int N = 305;
vector<pii> g[N];
int dp[N][N];

void dfs(int src, int par) {
    for (auto child : g[src]) {
        int ch = child.f, wt = child.s;
        if (ch == par) continue;
        dfs(ch, src);
        for (int l = 0; l < N; l++) {
            int mn = inf;
            for (int k = 0; k <= l; k++) {
                int diff = l-k;
                mn = min(mn, dp[ch][k] + abs(diff-wt));
            }
            dp[src][l] += mn;
            dp[src][l] = min(dp[src][l], inf);
        }
    }
    if (g[src].size() == 1 && src != 1) {
        // dp[src][0] = 0;
        for (int i = 1; i < N; i++) dp[src][i] = inf;
    }
}

void sol() {
    // memset(dp, 63, sizeof dp);
    int n, m; cin >> n >> m;
    vi lengths;
    for (int i = 2; i <= n+m; i++) {
        int x, length; cin >> x >> length;
        g[x].pb({i, length});
        g[i].pb({x, length});
        lengths.pb(length);
    }

    if (n == 1) {
        sort(lengths.begin(), lengths.end());
        int ans = 0;
        for (int i = 0; i < m; i++) {
            ans += abs(lengths[m / 2] - lengths[i]);
        }
        cout << ans;
        return;
    }

    dfs(1, -1);

    int ans = inf;
    for (int i = 0; i < N; i++) ans = min(ans, dp[1][i]);
    cout << ans << endl;
}

signed main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;
    // cin >> t; 
    while (t--) {
        sol();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 3 ms 348 KB Output is correct
3 Correct 3 ms 604 KB Output is correct
4 Correct 5 ms 604 KB Output is correct
5 Correct 7 ms 604 KB Output is correct
6 Correct 7 ms 728 KB Output is correct
7 Correct 8 ms 604 KB Output is correct
8 Correct 9 ms 872 KB Output is correct
9 Correct 12 ms 860 KB Output is correct
10 Correct 10 ms 996 KB Output is correct
11 Correct 13 ms 860 KB Output is correct
12 Correct 13 ms 860 KB Output is correct
13 Correct 13 ms 1116 KB Output is correct
14 Correct 15 ms 1120 KB Output is correct
15 Correct 14 ms 1116 KB Output is correct
16 Correct 18 ms 1372 KB Output is correct
17 Correct 14 ms 1124 KB Output is correct
18 Correct 14 ms 1116 KB Output is correct
19 Correct 14 ms 1120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 3 ms 348 KB Output is correct
13 Correct 3 ms 604 KB Output is correct
14 Correct 5 ms 604 KB Output is correct
15 Correct 7 ms 604 KB Output is correct
16 Correct 7 ms 728 KB Output is correct
17 Correct 8 ms 604 KB Output is correct
18 Correct 9 ms 872 KB Output is correct
19 Correct 12 ms 860 KB Output is correct
20 Correct 10 ms 996 KB Output is correct
21 Correct 13 ms 860 KB Output is correct
22 Correct 13 ms 860 KB Output is correct
23 Correct 13 ms 1116 KB Output is correct
24 Correct 15 ms 1120 KB Output is correct
25 Correct 14 ms 1116 KB Output is correct
26 Correct 18 ms 1372 KB Output is correct
27 Correct 14 ms 1124 KB Output is correct
28 Correct 14 ms 1116 KB Output is correct
29 Correct 14 ms 1120 KB Output is correct
30 Runtime error 1 ms 612 KB Execution killed with signal 11
31 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 3 ms 348 KB Output is correct
13 Correct 3 ms 604 KB Output is correct
14 Correct 5 ms 604 KB Output is correct
15 Correct 7 ms 604 KB Output is correct
16 Correct 7 ms 728 KB Output is correct
17 Correct 8 ms 604 KB Output is correct
18 Correct 9 ms 872 KB Output is correct
19 Correct 12 ms 860 KB Output is correct
20 Correct 10 ms 996 KB Output is correct
21 Correct 13 ms 860 KB Output is correct
22 Correct 13 ms 860 KB Output is correct
23 Correct 13 ms 1116 KB Output is correct
24 Correct 15 ms 1120 KB Output is correct
25 Correct 14 ms 1116 KB Output is correct
26 Correct 18 ms 1372 KB Output is correct
27 Correct 14 ms 1124 KB Output is correct
28 Correct 14 ms 1116 KB Output is correct
29 Correct 14 ms 1120 KB Output is correct
30 Runtime error 1 ms 612 KB Execution killed with signal 11
31 Halted 0 ms 0 KB -