Submission #406266

# Submission time Handle Problem Language Result Execution time Memory
406266 2021-05-17T10:00:55 Z talant117408 Fireworks (APIO16_fireworks) C++17
7 / 100
2 ms 972 KB
/*
    Code written by Talant I.D.
*/

#include <bits/stdc++.h>
//~ #include <ext/pb_ds/assoc_container.hpp>
 
//~ using namespace __gnu_pbds;
using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
//~ typedef tree <int, null_type, less_equal <int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
 
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define OK cout << "OK" << endl;
 
const int mod = 1e9+7;
 
ll mode(ll a) {
    while (a < 0) {
        a += mod;
    }
    return a % mod;
}
 
ll subt(ll a, ll b) {
    return mode(mode(a)-mode(b));
}
 
ll add(ll a, ll b) {
    return mode(mode(a)+mode(b));
}
 
ll mult(ll a, ll b) {
    return mode(mode(a)*mode(b));
}
 
ll binpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b&1) res = mult(res, a);
        a = mult(a, a);
        b >>= 1;
    }
    return res;
}

const int N = 303;
ll dp[N][N];
vector <pii> graph[N];

void dfs(int v, int p) {
    int children = 0;
    for (auto to : graph[v]) {
        if (to.first == p) {
            continue;
        }
        children++;
        dfs(to.first, v);
        for (int j = 0; j+to.second <= 300; j++) {
            auto &res = dp[v][j+to.second];
            for (int i = j; i <= 300; i++) {
                res = min(res, dp[to.first][i]+(j+to.second-i));
            }
            dp[v][j+to.second] = res;
        }
    }
    if (!children) {
        dp[v][0] = 0;
    }
}

int main() {
    do_not_disturb
    
    int n, m;
    cin >> n >> m;
    
    for (int i = 2; i <= n+m; i++) {
        int p, w;
        cin >> p >> w;
        graph[p].pb(mp(i, w));
        graph[i].pb(mp(p, w));
    }
    
    if (n == 1) {
        ll ans = 9e18;
        for (auto to : graph[1]) {
            ll sum = 0;
            for (auto to2 : graph[1]) {
                sum += abs(to.second-to2.second);
            }
            ans = min(ans, sum);
        }
        cout << ans << endl;
    }
    else if (n+m <= 300) {
        for (int i = 0; i <= 300; i++) {
            for (int j = 0; j <= 300; j++) {
                dp[i][j] = 1e18;
            }
        }
        dfs(1, 1);
        ll ans = 9e18;
        for (int i = 0; i <= 300; i++) {
            ans = min(ans, dp[1][i]);
        }
        cout << ans << endl;
    }
    
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 972 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Incorrect 2 ms 972 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Incorrect 2 ms 972 KB Output isn't correct
12 Halted 0 ms 0 KB -