#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll f(vector<int> &v) {
ll res = 0;
sort(v.begin(), v.end());
for (int j = 0; j < (int)v.size(); j++) res += (j < (int)v.size() >> 1 ? -1 : 1) * v[j];
return res;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int K, n;
cin >> K >> n;
ll sm = 0;
vector<vector<int>> v;
for (int i = 0; i < n; i++) {
char p, q;
int s, t;
cin >> p >> s >> q >> t;
if (p == q) sm += abs(t - s);
else v.push_back({s + t, s, t});
}
if (v.empty()) return cout << sm, 0;
sm += (int)v.size();
vector<int> u;
for (auto i : v) u.insert(u.end(), {i[1], i[2]});
if (K == 1) return cout << sm + f(u), 0;
ll mn = LLONG_MAX;
sort(v.begin(), v.end());
for (int i = 0; i < (int)v.size(); i++) {
vector<int> u1, u2;
for (int j = 0; j < (int)v.size(); j++) {
for (auto k : {v[j][1], v[j][2]}) {
(j <= i ? u1 : u2).push_back(k);
}
}
mn = min(mn, f(u1) + f(u2));
}
cout << sm + mn;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
20 ms |
8344 KB |
Output is correct |
13 |
Correct |
35 ms |
9988 KB |
Output is correct |
14 |
Correct |
27 ms |
8196 KB |
Output is correct |
15 |
Correct |
21 ms |
5900 KB |
Output is correct |
16 |
Correct |
25 ms |
9228 KB |
Output is correct |
17 |
Correct |
26 ms |
9860 KB |
Output is correct |
18 |
Correct |
31 ms |
9472 KB |
Output is correct |
19 |
Correct |
40 ms |
9988 KB |
Output is correct |
20 |
Correct |
26 ms |
9480 KB |
Output is correct |
21 |
Correct |
33 ms |
9740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
464 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
18 ms |
556 KB |
Output is correct |
14 |
Correct |
62 ms |
348 KB |
Output is correct |
15 |
Correct |
75 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
19 ms |
348 KB |
Output is correct |
18 |
Correct |
10 ms |
348 KB |
Output is correct |
19 |
Correct |
20 ms |
344 KB |
Output is correct |
20 |
Correct |
26 ms |
344 KB |
Output is correct |
21 |
Correct |
43 ms |
348 KB |
Output is correct |
22 |
Correct |
26 ms |
348 KB |
Output is correct |
23 |
Correct |
20 ms |
348 KB |
Output is correct |
24 |
Correct |
27 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
448 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
18 ms |
348 KB |
Output is correct |
14 |
Correct |
62 ms |
348 KB |
Output is correct |
15 |
Correct |
76 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
344 KB |
Output is correct |
17 |
Correct |
19 ms |
524 KB |
Output is correct |
18 |
Correct |
10 ms |
348 KB |
Output is correct |
19 |
Correct |
20 ms |
560 KB |
Output is correct |
20 |
Correct |
26 ms |
344 KB |
Output is correct |
21 |
Correct |
43 ms |
348 KB |
Output is correct |
22 |
Correct |
26 ms |
348 KB |
Output is correct |
23 |
Correct |
19 ms |
556 KB |
Output is correct |
24 |
Correct |
27 ms |
348 KB |
Output is correct |
25 |
Execution timed out |
2041 ms |
9732 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |