//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define ss second
#define ff first
#define vt vector
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll inf = 1e18;
const int mod = 1e9+7;
const int maxn = 1e6 + 1;
bool cmp(pll x, pll y){
return x.ff + x.ss < y.ff + y.ss;
}
void solve() {
int k, n;
cin >> k >> n;
ll ans = 0, s = inf;
vt<pair<ll, ll>> v;
for(int i = 0; i < n; i++){
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if(x > y)swap(x, y);
if(a != b){
v.pb({x, y});
ans++;
}
else ans += y - x;
}
if(v.empty()){
cout << ans;
return;
}
sort(all(v), cmp);
n = v.size();
ll dp[n], wt = 0;
multiset<ll> m1, m2;
for(int i = 0; i < n; i++){
m1.insert(v[i].ff), m2.insert(v[i].ss);
wt += v[i].ss - v[i].ff;
while(*m2.begin() < *m1.rbegin()){
int x = *m1.rbegin(), y = *m2.begin();
wt += (x - y) * 2;
m2.erase(m2.begin()), m1.erase(--m1.end());
m2.insert(x), m1.insert(y);
}
dp[i] = wt;
}
s = dp[n - 1];
m1.clear(), m2.clear();
wt = 0;
for(int i = n - 1; k == 2 && i >= 0; i--){
m1.insert(v[i].ff), m2.insert(v[i].ss);
wt += v[i].ss - v[i].ff;
while(*m2.begin() < *m1.rbegin()){
int x = *m1.rbegin(), y = *m2.begin();
wt += (x - y) * 2;
m2.erase(m2.begin()), m1.erase(--m1.end());
m2.insert(x), m1.insert(y);
}
if(i)s = min(s, dp[i - 1] + wt);
}
cout << ans + s << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int times = 1;
//cin >> times;
for(int i = 1; i <= times; i++) {
solve();
}
return 0;
}
# |
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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
468 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
484 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 |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
51 ms |
12952 KB |
Output is correct |
13 |
Correct |
96 ms |
14152 KB |
Output is correct |
14 |
Correct |
78 ms |
11924 KB |
Output is correct |
15 |
Correct |
51 ms |
8664 KB |
Output is correct |
16 |
Correct |
50 ms |
13644 KB |
Output is correct |
17 |
Correct |
79 ms |
14276 KB |
Output is correct |
18 |
Correct |
44 ms |
13964 KB |
Output is correct |
19 |
Correct |
82 ms |
14440 KB |
Output is correct |
20 |
Correct |
67 ms |
13768 KB |
Output is correct |
21 |
Correct |
80 ms |
14024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
600 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
592 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 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 |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
468 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 |
0 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 |
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 |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
512 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 |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
81 ms |
12804 KB |
Output is correct |
26 |
Correct |
149 ms |
12996 KB |
Output is correct |
27 |
Correct |
157 ms |
13000 KB |
Output is correct |
28 |
Correct |
160 ms |
13288 KB |
Output is correct |
29 |
Correct |
161 ms |
13380 KB |
Output is correct |
30 |
Correct |
71 ms |
7376 KB |
Output is correct |
31 |
Correct |
78 ms |
13004 KB |
Output is correct |
32 |
Correct |
124 ms |
13304 KB |
Output is correct |
33 |
Correct |
74 ms |
13172 KB |
Output is correct |
34 |
Correct |
122 ms |
13324 KB |
Output is correct |
35 |
Correct |
117 ms |
13000 KB |
Output is correct |
36 |
Correct |
127 ms |
13408 KB |
Output is correct |
37 |
Correct |
71 ms |
12484 KB |
Output is correct |