This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://www.hackerrank.com/contests/world-codesprint-5/challenges/mining/problem
#include "dp/knuth_partition.hpp"
int k, n, w[1000000], x[1000000];
ll cost[5001][5001];
int main() {
scanf("%d%d", &n, &k);
rep(i, n) scanf("%d%d", &x[i], &w[i]);
rep(l, n) {
int k = l;
ll wl = w[l], wr = 0;
ll c = cost[l][l + 1] = 0;
rep2(r, l + 1, n) {
wr += w[r], c += 1LL * (x[r] - x[k]) * w[r];
while (k < r && wl < wr) {
c = c + (wl - wr) * (x[k + 1] - x[k]);
k++;
wl += w[k], wr -= w[k];
}
cost[l][r + 1] = c;
}
}
auto dp = knuth_partition_dp(k, n, [](int l, int r) { return cost[l][r]; });
printf("%lld", dp[n]);
}
#line 1 "test/dp/knuth_partition.test.cpp"
// competitive-verifier: PROBLEM https://www.hackerrank.com/contests/world-codesprint-5/challenges/mining/problem
#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "dp/knuth_partition.hpp"
// O(kn + n^2)
// Minimizes cost of k-partitions of [0, n) with k <= max_k
// (k == max_k if cost(i,i) = inf).
// Requires cost(a,c) + cost(b,d) <= cost(a,d) + cost(b,c)
// for a < b < c < d (wider is worse).
template <class F>
auto knuth_partition_dp(int max_k, int n, F cost) {
using T = decltype(cost(0, 0));
vector<T> dp(n + 1), dp2(n + 1);
vector<int> opt(n + 2), opt2(n + 2);
rep(i, n + 1) dp[i] = cost(0, i);
rep2(k, 1, max_k) {
fill(all(dp2), numeric_limits<T>::max() / 2);
opt2[n+1] = n;
repr(i, n + 1) rep2(j, opt[i], min(i, opt2[i + 1]) + 1) {
if (dp2[i] > dp[j] + cost(j, i)) dp2[i] = dp[j] + cost(j, i), opt2[i] = j;
}
swap(dp, dp2);
swap(opt, opt2);
}
return dp;
}
#line 4 "test/dp/knuth_partition.test.cpp"
int k, n, w[1000000], x[1000000];
ll cost[5001][5001];
int main() {
scanf("%d%d", &n, &k);
rep(i, n) scanf("%d%d", &x[i], &w[i]);
rep(l, n) {
int k = l;
ll wl = w[l], wr = 0;
ll c = cost[l][l + 1] = 0;
rep2(r, l + 1, n) {
wr += w[r], c += 1LL * (x[r] - x[k]) * w[r];
while (k < r && wl < wr) {
c = c + (wl - wr) * (x[k + 1] - x[k]);
k++;
wl += w[k], wr -= w[k];
}
cost[l][r + 1] = c;
}
}
auto dp = knuth_partition_dp(k, n, [](int l, int r) { return cost[l][r]; });
printf("%lld", dp[n]);
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++-12 | 00 |
![]() |
30 ms | 12 MB |
g++-12 | 01 |
![]() |
29 ms | 12 MB |
g++-12 | 02 |
![]() |
30 ms | 12 MB |
g++-12 | 03 |
![]() |
29 ms | 12 MB |
g++-12 | 04 |
![]() |
30 ms | 12 MB |
g++-12 | 05 |
![]() |
29 ms | 14 MB |
g++-12 | 06 |
![]() |
29 ms | 14 MB |
g++-12 | 07 |
![]() |
29 ms | 12 MB |
g++-12 | 08 |
![]() |
30 ms | 12 MB |
g++-12 | 09 |
![]() |
30 ms | 12 MB |
g++-12 | 10 |
![]() |
30 ms | 12 MB |
g++-12 | 11 |
![]() |
32 ms | 18 MB |
g++-12 | 12 |
![]() |
37 ms | 28 MB |
g++-12 | 13 |
![]() |
58 ms | 45 MB |
g++-12 | 14 |
![]() |
45 ms | 39 MB |
g++-12 | 15 |
![]() |
38 ms | 29 MB |
g++-12 | 16 |
![]() |
36 ms | 30 MB |
g++-12 | 17 |
![]() |
55 ms | 45 MB |
g++-12 | 18 |
![]() |
42 ms | 37 MB |
g++-12 | 19 |
![]() |
108 ms | 70 MB |
g++-12 | 20 |
![]() |
76 ms | 63 MB |
g++-12 | 21 |
![]() |
171 ms | 102 MB |
g++-12 | 22 |
![]() |
199 ms | 129 MB |
g++-12 | 23 |
![]() |
71 ms | 55 MB |
g++-12 | 24 |
![]() |
117 ms | 86 MB |
g++-12 | 25 |
![]() |
925 ms | 189 MB |
g++-12 | 26 |
![]() |
321 ms | 149 MB |
g++-12 | 27 |
![]() |
954 ms | 206 MB |
g++-12 | 28 |
![]() |
1160 ms | 207 MB |
g++-12 | 29 |
![]() |
1031 ms | 207 MB |
g++-12 | 30 |
![]() |
1041 ms | 207 MB |
g++-12 | 31 |
![]() |
1099 ms | 207 MB |
g++-12 | 32 |
![]() |
1110 ms | 205 MB |
g++-12 | 33 |
![]() |
730 ms | 206 MB |
g++-12 | 34 |
![]() |
907 ms | 206 MB |
g++-12 | 35 |
![]() |
1212 ms | 207 MB |
g++-12 | 36 |
![]() |
1171 ms | 205 MB |
g++-12 | 37 |
![]() |
1193 ms | 206 MB |
g++-12 | 38 |
![]() |
1166 ms | 206 MB |
g++-12 | 39 |
![]() |
49 ms | 51 MB |