int lst = 0; vector<vector<int>> dp(H + 1, vector<int>(H + 1, 0)); for (int i = 0; i < N; i++) { int d = X[i] - lst; lst = X[i];
vector<vector<int>> g(H + 1, vector<int>(H + 1, inf)); for (int x = d; x <= H; x++) { for (int y = d; y <= H; y++) { cmin(g[x - d][y], dp[x][y - d]); } } dp = move(g); if (i < N - 1) { int P, F; cin >> P >> F; g = dp; for (int x = 0; x <= H; x++) { for (int y = 0; y <= H; y++) { cmin(g[min(x + F, H)][y], dp[x][y] + P); cmin(g[x][y], dp[x][min(y + F, H)] + P); } } dp = move(g); } } int ans = inf; for (int i = 0; i <= H; i++) { cmin(ans, dp[i][i]); }
int ans = -1; for (int c = 0; c < 10; c++) { debug(c); vector<vi> adj(M); vector<int> q; bool ok = true; for (int i = 0; i < N; i++) { if (p[c][i].empty()) { ok = false; } for (auto j : p[c][i]) { q.pb(j); adj[j].pb(i); } } if (!ok) { continue; } sort(all(q)); q.resize(unique(all(q)) - q.begin()); int res = 0; vi mc(N, -1); vi vis(M); auto find = [&](auto self, int u) -> bool { vis[u] = 1; for (auto v : adj[u]) { if (mc[v] == -1 || (!vis[mc[v]] && self(self, mc[v]))) { mc[v] = u; returntrue; } } returnfalse; }; for (int t = 0;; t++) { for (auto i : q) { if (find(find, i)) { res += 1; if (res == N) { int v = i + t * M; if (ans == -1 || ans > v) { ans = v; break; } } vis.assign(N, 0); } } if (res == N) { break; } } }