Submission #962554

# Submission time Handle Problem Language Result Execution time Memory
962554 2024-04-13T19:30:37 Z BhavayGoyal Fireworks (APIO16_fireworks) C++14
19 / 100
15 ms 1116 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;
    int sum = 0;
    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});
        sum += length;
        lengths.pb(length);
    }

    auto get = [&](int mid) {
        int sum = 0;
        for (auto i : lengths) {
            sum += abs(mid-i);
        }
        return sum;
    };

    if (n == 1) {
        int ans = min({get(sum/m), get(sum/m+1), get(sum/m-1)});
        cout << ans << endl;
        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 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 3 ms 592 KB Output is correct
4 Correct 5 ms 604 KB Output is correct
5 Correct 6 ms 604 KB Output is correct
6 Correct 6 ms 604 KB Output is correct
7 Correct 8 ms 856 KB Output is correct
8 Correct 9 ms 860 KB Output is correct
9 Correct 9 ms 860 KB Output is correct
10 Correct 10 ms 860 KB Output is correct
11 Correct 11 ms 860 KB Output is correct
12 Correct 13 ms 860 KB Output is correct
13 Correct 13 ms 892 KB Output is correct
14 Correct 14 ms 1116 KB Output is correct
15 Correct 14 ms 1116 KB Output is correct
16 Correct 14 ms 1116 KB Output is correct
17 Correct 14 ms 1112 KB Output is correct
18 Correct 15 ms 1116 KB Output is correct
19 Correct 15 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -