/** Im the best because i work as hard as i possibly can **/
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define Mp make_pair
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl "\n"
#define SZ(x) (int)x.size()
const int N = 1e6 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 22;
ll pw(ll a , ll b, ll M) { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }
int n, k;
ll base, tot, pre[N], suf[N], L, R;
vector < pii > vec;
priority_queue < int > lq;
priority_queue < int, vector < int > , greater < int > > rq;
bool cmp(pii a, pii b)
{
return (a.F + a.S) < (b.F + b.S);
}
void add(int x)
{
if(lq.empty() || x <= lq.top())
{
lq.push(x);
L += x;
}
else
{
rq.push(x);
R += x;
}
if(SZ(lq) - 1 > SZ(rq))
{
int z = lq.top();
lq.pop();
rq.push(z);
L -= z, R += z;
}
if(SZ(lq) < SZ(rq))
{
int z = rq.top();
rq.pop();
lq.push(z);
L += z, R -= z;
}
}
int main()
{
fast_io;
cin >> k >> n;
for(int i = 1; i <= n; i ++)
{
string a, b;
int x, y;
cin >> a >> x >> b >> y;
base += (a == b? abs(x - y) : 1);
if(a != b)
{
vec.push_back(Mp(min(x, y), max(x, y)));
}
}
sort(all(vec), cmp);
for(int i = 0; i < SZ(vec); i ++)
{
add(vec[i].F);
add(vec[i].S);
pre[i + 1] = R - L;
///cout << vec[i].F << " " << vec[i].S << " " << L << " " << R << endl;
}
tot = pre[SZ(vec)];
if(k ^ 1)
{
while(SZ(lq)) lq.pop();
while(SZ(rq)) rq.pop();
L = 0;
R = 0;
for(int i = SZ(vec) - 1; ~i; i --)
{
add(vec[i].F);
add(vec[i].S);
tot = min(tot, R - L + pre[i]);
}
}
cout << tot + base;
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 |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
42 ms |
3944 KB |
Output is correct |
13 |
Correct |
69 ms |
5608 KB |
Output is correct |
14 |
Correct |
61 ms |
4328 KB |
Output is correct |
15 |
Correct |
40 ms |
3564 KB |
Output is correct |
16 |
Correct |
52 ms |
5004 KB |
Output is correct |
17 |
Correct |
63 ms |
5572 KB |
Output is correct |
18 |
Correct |
49 ms |
5352 KB |
Output is correct |
19 |
Correct |
68 ms |
5480 KB |
Output is correct |
20 |
Correct |
53 ms |
5608 KB |
Output is correct |
21 |
Correct |
66 ms |
5224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
396 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
54 ms |
3944 KB |
Output is correct |
26 |
Correct |
80 ms |
4480 KB |
Output is correct |
27 |
Correct |
101 ms |
5432 KB |
Output is correct |
28 |
Correct |
103 ms |
5992 KB |
Output is correct |
29 |
Correct |
105 ms |
6116 KB |
Output is correct |
30 |
Correct |
54 ms |
3308 KB |
Output is correct |
31 |
Correct |
58 ms |
4840 KB |
Output is correct |
32 |
Correct |
91 ms |
5480 KB |
Output is correct |
33 |
Correct |
67 ms |
5352 KB |
Output is correct |
34 |
Correct |
94 ms |
5480 KB |
Output is correct |
35 |
Correct |
67 ms |
5524 KB |
Output is correct |
36 |
Correct |
93 ms |
5224 KB |
Output is correct |
37 |
Correct |
51 ms |
3944 KB |
Output is correct |