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"
using namespace std;
using ll = long long;
const ll inf = 1e18;
void mins(pair<int, ll> &a, pair<int, ll> b) {
if (a.first == b.first) {
if (a.second > b.second) {
a = b;
}
} else if (a.first < b.first) {
a = b;
}
}
void solve() {
int n, k;
cin >> n >> k;
if (k > 2) {
cout << 0;
return;
}
vector<ll> dp(n + 1, inf);
vector<pair<int, int>> a(n);
dp[0] = 0;
for (int i = 0; i < n; i++) {
cin >> a[i].second >> a[i].first;
}
sort(a.begin(), a.end());
for (int i = 0; i < n; i++) {
auto [d, w] = a[i];
for (int j = d - 1; j >= 0; j--) {
dp[j + 1] = min(dp[j + 1], dp[j] + w);
}
}
int ans_pos;
for (int i = n; i > 0; i--) {
if (dp[i] != inf) {
ans_pos = i;
cout << i << ' ' << dp[i] << '\n';
break;
}
}
if (k == 1) {
return;
}
vector<int> pos;
vector<bool> vis(n);
{
for (int i = ans_pos; i > 0; i--) {
for (int j = n - 1; j >= 0; j--) {
if (!vis[j] and a[j].first >= i and dp[i - 1] + a[j].second == dp[i]) {
vis[j] = true;
pos.emplace_back(j);
break;
}
}
}
}
reverse(pos.begin(), pos.end());
pair<int, ll> best = {0, 0};
for (int i = 0; i < ans_pos; i++) {
int x = pos[i];
pos.erase(pos.begin() + i);
vector<bool> suff(pos.size() + 1, true);
for (int j = (int) pos.size() - 1; j >= 0; j--) {
suff[j] = suff[j + 1] & (a[pos[j]].first > j + 1);
}
mins(best, make_pair(ans_pos - 1, dp[ans_pos] - a[x].second));
for (int j = 0; j < n; j++) {
if (vis[j]) {
continue;
}
int l = lower_bound(pos.begin(), pos.end(), j) - pos.begin();
if (a[j].first >= l + 1 and suff[l]) {
mins(best, make_pair(ans_pos, dp[ans_pos] - a[x].second + a[j].second));
}
}
pos.insert(pos.begin() + i, x);
}
cout << best.first << ' ' << best.second;
}
int main() {
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
srand(time(0));
int queries = 1;
#ifdef test_cases
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
cin >> queries;
#else
// cin >> queries;
#endif
for (int test_case = 1; test_case <= queries; test_case++) {
#ifdef test_cases
cout << "Test case: " << test_case << endl;
#endif
solve();
cout << '\n';
}
}
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:12:12: warning: 'ans_pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
12 | } else if (a.first < b.first) {
| ^~
Main.cpp:37:9: note: 'ans_pos' was declared here
37 | int ans_pos;
| ^~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |