#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 = 1; i < (int)v.size() - 1; 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 |
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 |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
19 ms |
7692 KB |
Output is correct |
13 |
Correct |
35 ms |
7680 KB |
Output is correct |
14 |
Correct |
27 ms |
6920 KB |
Output is correct |
15 |
Correct |
22 ms |
4624 KB |
Output is correct |
16 |
Correct |
24 ms |
7672 KB |
Output is correct |
17 |
Correct |
34 ms |
7684 KB |
Output is correct |
18 |
Correct |
31 ms |
7684 KB |
Output is correct |
19 |
Correct |
32 ms |
7688 KB |
Output is correct |
20 |
Correct |
23 ms |
7688 KB |
Output is correct |
21 |
Correct |
30 ms |
7716 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 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 |
1 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 |
344 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 |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
19 ms |
548 KB |
Output is correct |
14 |
Correct |
61 ms |
348 KB |
Output is correct |
15 |
Correct |
74 ms |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
344 KB |
Output is correct |
17 |
Correct |
19 ms |
348 KB |
Output is correct |
18 |
Correct |
10 ms |
348 KB |
Output is correct |
19 |
Correct |
19 ms |
544 KB |
Output is correct |
20 |
Correct |
24 ms |
348 KB |
Output is correct |
21 |
Correct |
46 ms |
348 KB |
Output is correct |
22 |
Correct |
24 ms |
344 KB |
Output is correct |
23 |
Correct |
19 ms |
348 KB |
Output is correct |
24 |
Correct |
24 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 |
348 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 |
348 KB |
Output is correct |
14 |
Correct |
61 ms |
348 KB |
Output is correct |
15 |
Correct |
75 ms |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
344 KB |
Output is correct |
17 |
Correct |
19 ms |
516 KB |
Output is correct |
18 |
Correct |
10 ms |
348 KB |
Output is correct |
19 |
Correct |
19 ms |
348 KB |
Output is correct |
20 |
Correct |
24 ms |
348 KB |
Output is correct |
21 |
Correct |
44 ms |
348 KB |
Output is correct |
22 |
Correct |
24 ms |
348 KB |
Output is correct |
23 |
Correct |
18 ms |
344 KB |
Output is correct |
24 |
Correct |
25 ms |
344 KB |
Output is correct |
25 |
Execution timed out |
2100 ms |
8848 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |