#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
// #define fisier 1
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;
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()
{
#ifdef fisier
ifstream f("input.in");
ofstream g("output.out");
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int k, n;
ll ansa = 0;
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 |
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 |
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 |
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 |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
32 ms |
3960 KB |
Output is correct |
13 |
Correct |
65 ms |
5616 KB |
Output is correct |
14 |
Correct |
50 ms |
4592 KB |
Output is correct |
15 |
Correct |
41 ms |
3572 KB |
Output is correct |
16 |
Correct |
43 ms |
5040 KB |
Output is correct |
17 |
Correct |
53 ms |
5488 KB |
Output is correct |
18 |
Correct |
59 ms |
5484 KB |
Output is correct |
19 |
Correct |
60 ms |
5496 KB |
Output is correct |
20 |
Correct |
47 ms |
5416 KB |
Output is correct |
21 |
Correct |
57 ms |
5232 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 |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 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 |
400 KB |
Output is correct |
12 |
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 |
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 |
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 |
512 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 |
432 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 |
4 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 |
6 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 |
25 |
Correct |
46 ms |
3952 KB |
Output is correct |
26 |
Correct |
77 ms |
4748 KB |
Output is correct |
27 |
Correct |
101 ms |
5236 KB |
Output is correct |
28 |
Correct |
107 ms |
5744 KB |
Output is correct |
29 |
Correct |
107 ms |
5488 KB |
Output is correct |
30 |
Correct |
56 ms |
3060 KB |
Output is correct |
31 |
Correct |
55 ms |
4832 KB |
Output is correct |
32 |
Correct |
84 ms |
5488 KB |
Output is correct |
33 |
Correct |
83 ms |
5104 KB |
Output is correct |
34 |
Correct |
89 ms |
5484 KB |
Output is correct |
35 |
Correct |
60 ms |
5488 KB |
Output is correct |
36 |
Correct |
85 ms |
5232 KB |
Output is correct |
37 |
Correct |
39 ms |
3952 KB |
Output is correct |