This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#ifdef local
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) qqbx(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
int cnt = sizeof...(T);
((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : "\033[0m)\n")));
}
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#endif // local
#define all(v) begin(v),end(v)
using namespace std;
const int64_t INF = 1e18;
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int64_t,int64_t>> cake(n);
for (auto &[c, v]: cake) cin >> v >> c;
sort(all(cake));
auto calc = [&](int64_t cost) -> pair<int64_t, int> {
pair<int64_t, int> dp[3];
dp[0] = {-INF, -n*2};
dp[1] = dp[2] = {-INF, -n*2};
for (auto [c, v]: cake) {
v -= cost;
dp[2] = max(dp[2], make_pair(dp[1].first + v - c * 2, dp[1].second + 1));
dp[1] = max(dp[1], make_pair(dp[1].first + v, dp[1].second + 1));
dp[1] = max(dp[1], make_pair(dp[0].first + v, dp[0].second + 1));
dp[0] = max(dp[0], make_pair(v + c * 2, 1));
}
return dp[2];
};
int64_t cost = (1LL << 60);
for (int64_t s = 1LL << 60; s; s>>=1) {
if (calc(cost - s).second <= m) {
cost -= s;
}
}
// --cost;
// ++cost;
debug(cost);
auto [dp, k] = calc(cost);
cout << dp + 1LL * k * cost << '\n';
/*
vector<int64_t> dp(n, -1e18);
vector<int64_t> ans(n+1, -1e18);
for (int i = 0; i < n; i++) {
auto [c, v] = cake[i];
// ans = max(ans, dp[m-1] + v - c * 2);
for (int j = 3; j <= n; j++)
ans[j] = max(ans[j], dp[j-1] + v - c * 2);
for (int j = n-2; j >= 1; j--) {
dp[j+1] = max(dp[j+1], dp[j] + v);
}
dp[1] = max(dp[1], v + c * 2);
}
cerr << "==========\n";
for (int j = 3; j <= n; j++)
cerr << ans[j] - ans[j-1] << '\n';
cout << ans[m] << '\n';
*/
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |