Submission #962554

#TimeUsernameProblemLanguageResultExecution timeMemory
962554BhavayGoyalFireworks (APIO16_fireworks)C++14
19 / 100
15 ms1116 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...