Submission #35668

#TimeUsernameProblemLanguageResultExecution timeMemory
35668funcsrFireworks (APIO16_fireworks)C++14
26 / 100
29 ms9560 KiB
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
//#define INF 1145141919
#define INF 1000101441
#define MOD 1000000007
inline void chmin(int &x, int v) { if (x > v) x = v; }

int N, M;
vector<P> G[300000];
int dp[300][301];
int T[301];

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> N >> M;
  rep(i, N-1+M) {
    int p, c;
    cin >> p >> c;
    p--;
    G[p].pb(P(i+1, c));
  }
  if (N == 1) {
    vector<int> costs;
    for (P p : G[0]) costs.pb(p._2);
    sort(all(costs));
    int g = costs[costs.size()/2];
    long long sum = 0;
    for (int c : costs) sum += abs(c - g);
    cout << sum << "\n";
  }
  else {
    if (N+M > 300) return 1;
    rep(i, N+M) rep(j, 301) dp[i][j] = INF;
    for (int x=N; x<N+M; x++) dp[x][0] = 0;
    for (int x=N-1; x>=0; x--) {
      rep(j, 301) dp[x][j] = 0;
      for (P p : G[x]) {
        rep(j, 301) T[j] = INF;
        int t = p._1, r = p._2;
        for (int c=0; c<=300; c++) {
          for (int l=0; c+l<=300; l++) {
            chmin(T[c+l], dp[t][c] + abs(r-l));
          }
        }
        rep(j, 301) dp[x][j] = min(dp[x][j]+T[j], INF);
      }
    }
    int m = INF;
    rep(i, 301) chmin(m, dp[0][i]);
    cout << m << "\n";
  }
  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...