#include<bits/stdc++.h>
#define god dimasi5eks
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const double dancila = 3.14159265359; // PI
const double eps = 1e-9;
bool cmp(pair<int, int> a, pair<int, int> b)
{
return a.fi + a.se < b.fi + b.se;
}
priority_queue<int> lpq;
priority_queue<int, vector<int>, greater<int>> rpq;
ll lsum, rsum, ansa;
ll ps[100001];
void add(int x)
{
int median = (lpq.size() ? lpq.top() : 1000000001);
if(x <= median)
{
lpq.push(x);
lsum += x;
}
else
{
rpq.push(x);
rsum += x;
}
if(rpq.size() + 1 < lpq.size())
{
int nxt = lpq.top();
lpq.pop();
rpq.push(nxt);
lsum -= nxt;
rsum += nxt;
}
else
if(lpq.size() < rpq.size())
{
int nxt = rpq.top();
rpq.pop();
lpq.push(nxt);
rsum -= nxt;
lsum += nxt;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int k, n;
vector<pair<int, int>> v = {{0, 0}};
cin >> k >> n;
for(int i = 0; i < n; ++i)
{
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if(a == b)
ansa += abs(x - y);
else
v.push_back({x, y});
}
sort(v.begin(), v.end(), cmp);
n = v.size() - 1;
ansa += n;
lsum = rsum = 0;
for(int i = 1; i <= n; ++i)
{
add(v[i].fi);
add(v[i].se);
ps[i] = rsum - lsum;
}
ll ansb = ps[n];
if(k == 2)
{
while(lpq.size())
lpq.pop();
while(rpq.size())
rpq.pop();
lsum = rsum = 0;
for(int i = n; i >= 1; i--)
{
add(v[i].fi);
add(v[i].se);
ansb = min(ansb, rsum - lsum + ps[i - 1]);
}
}
cout << ansa + ansb;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
34 ms |
3568 KB |
Output is correct |
13 |
Correct |
68 ms |
3696 KB |
Output is correct |
14 |
Correct |
51 ms |
3820 KB |
Output is correct |
15 |
Correct |
40 ms |
2548 KB |
Output is correct |
16 |
Correct |
42 ms |
3696 KB |
Output is correct |
17 |
Correct |
53 ms |
3564 KB |
Output is correct |
18 |
Correct |
52 ms |
3952 KB |
Output is correct |
19 |
Correct |
61 ms |
3568 KB |
Output is correct |
20 |
Correct |
45 ms |
3952 KB |
Output is correct |
21 |
Correct |
58 ms |
3564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
436 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
46 ms |
3568 KB |
Output is correct |
26 |
Correct |
85 ms |
4080 KB |
Output is correct |
27 |
Correct |
102 ms |
3824 KB |
Output is correct |
28 |
Correct |
107 ms |
3868 KB |
Output is correct |
29 |
Correct |
106 ms |
3568 KB |
Output is correct |
30 |
Correct |
58 ms |
2292 KB |
Output is correct |
31 |
Correct |
60 ms |
3568 KB |
Output is correct |
32 |
Correct |
81 ms |
3568 KB |
Output is correct |
33 |
Correct |
82 ms |
3568 KB |
Output is correct |
34 |
Correct |
86 ms |
3564 KB |
Output is correct |
35 |
Correct |
61 ms |
3948 KB |
Output is correct |
36 |
Correct |
83 ms |
3568 KB |
Output is correct |
37 |
Correct |
39 ms |
3568 KB |
Output is correct |