// In the name of the God
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 1e5+5;
int k, n, sd = 0, su = 0, dp[maxn];
vector <pair <int, pii> > vec;
multiset <int> ali, stu;
void reset() {
if (stu.size() > ali.size()) {
int x = *stu.begin();
stu.erase(stu.begin());
su -= x;
ali.insert(x);
sd += x;
}
if (ali.size()-1 > stu.size()) {
int x = *ali.rbegin();
ali.erase(ali.find(x));
sd -= x;
stu.insert(x);
su += x;
}
}
void add(int x) {
int m = ali.size() ? *ali.rbegin() : inf;
if (x <= m) {
ali.insert(x); sd += x;
}
else {
stu.insert(x); su += x;
}
reset();
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> k >> n;
int res = 0;
for (int i = 0; i < n; i++) {
char p, q; int s, t; cin >> p >> s >> q >> t;
if (p == q) res += abs(s-t);
else {
res++;
vec.pb(mp(s+t, mp(s, t)));
}
}
n = vec.size();
sort(all(vec));
for (int i = 0; i < n; i++) {
int s = vec[i].S.F, t = vec[i].S.S;
add(s), add(t);
dp[i] = su-sd;
}
int ans = dp[n-1];
if (k == 2) {
sd = su = 0;
ali.clear(), stu.clear();
for (int i = n-1; i >= 1; i--) {
int s = vec[i].S.F, t = vec[i].S.S;
add(s), add(t);
smin(ans, su-sd+dp[i-1]);
}
}
cout << ans+res << '\n';
return 0;
}
/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
444 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
480 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
484 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
62 ms |
13712 KB |
Output is correct |
13 |
Correct |
89 ms |
15836 KB |
Output is correct |
14 |
Correct |
79 ms |
13248 KB |
Output is correct |
15 |
Correct |
48 ms |
8904 KB |
Output is correct |
16 |
Correct |
65 ms |
15036 KB |
Output is correct |
17 |
Correct |
59 ms |
15128 KB |
Output is correct |
18 |
Correct |
83 ms |
15224 KB |
Output is correct |
19 |
Correct |
67 ms |
15956 KB |
Output is correct |
20 |
Correct |
65 ms |
14568 KB |
Output is correct |
21 |
Correct |
63 ms |
14784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
468 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 |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
480 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
121 ms |
13752 KB |
Output is correct |
26 |
Correct |
118 ms |
14016 KB |
Output is correct |
27 |
Correct |
150 ms |
14528 KB |
Output is correct |
28 |
Correct |
145 ms |
15004 KB |
Output is correct |
29 |
Correct |
175 ms |
15200 KB |
Output is correct |
30 |
Correct |
73 ms |
8044 KB |
Output is correct |
31 |
Correct |
97 ms |
14268 KB |
Output is correct |
32 |
Correct |
92 ms |
14780 KB |
Output is correct |
33 |
Correct |
117 ms |
14588 KB |
Output is correct |
34 |
Correct |
114 ms |
14668 KB |
Output is correct |
35 |
Correct |
114 ms |
14216 KB |
Output is correct |
36 |
Correct |
99 ms |
14668 KB |
Output is correct |
37 |
Correct |
102 ms |
13500 KB |
Output is correct |