#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
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 |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
356 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
19 ms |
612 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
356 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Runtime error |
19 ms |
612 KB |
Execution killed with signal 11 |
28 |
Halted |
0 ms |
0 KB |
- |