#include <bits/stdc++.h>
#define INF int64_t(1e9)
#define pii pair <int, int>
#define pll pair <long long, long long>
#define mp make_pair
#define file "test"
using namespace std;
const int nmax = 2e5 + 5;
const long long mod = 1e9 + 7;
int n,k;
long long sLow = 0 , sUp = 0, pref[nmax];
long long res = 0;
multiset <int> low, up;
vector <pii> v;
bool cmp(pii a, pii b)
{
return (a.first + a.second < b.first + b.second);
}
void add(int val)
{
int a = (low.size() ? *low.rbegin() : INF+5);
if (val <= a)
{
sLow += val;
low.insert(val);
}
else
{
sUp += val;
up.insert(val);
}
if (up.size() + 1 < low.size())
{
int w = *low.rbegin();
up.insert(w);
low.erase(low.find(w));
sUp += w;
sLow -= w;
}
else if (low.size() < up.size())
{
int w = *up.begin();
low.insert(w);
up.erase(up.find(w));
sUp -= w;
sLow += w;
}
}
void ers(int val)
{
if (up.find(val) != up.end()) up.erase(up.find(val)),sUp-=val;
else low.erase(low.find(val)),sLow-=val;
if (low.empty())
{
low.insert(*up.begin());
sLow+=*up.begin();
sUp-=*up.begin();
up.erase(up.find(*up.begin()));
}
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
//freopen(file".inp","r",stdin);
//freopen(file".out","w",stdout);
cin >> k >> n;
v.push_back(mp(0,0));
for (int i=1;i<=n;i++)
{
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if (a == b) res += abs(x-y);
else
{
v.push_back(mp(x,y));
}
}
n = v.size() - 1;
res+=n;
sort(v.begin(), v.end(), cmp);
for (int i=1;i<=n+1;i++)
{
add(v[i].first);
add(v[i].second);
pref[i] = sUp - sLow;
}
long long ans = pref[n];
if (k == 2)
{
up.clear();
low.clear();
sUp = 0;
sLow = 0;
for (int i=n;i;i--)
{
add(v[i].first);
add(v[i].second);
ans = min(ans, sUp - sLow + pref[i-1]);
}
}
cout << ans + res ;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 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 |
344 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 |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
500 KB |
Output is correct |
6 |
Correct |
1 ms |
404 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 |
62 ms |
12184 KB |
Output is correct |
13 |
Correct |
106 ms |
13516 KB |
Output is correct |
14 |
Correct |
87 ms |
11408 KB |
Output is correct |
15 |
Correct |
59 ms |
8144 KB |
Output is correct |
16 |
Correct |
65 ms |
12996 KB |
Output is correct |
17 |
Correct |
64 ms |
13516 KB |
Output is correct |
18 |
Correct |
53 ms |
13260 KB |
Output is correct |
19 |
Correct |
67 ms |
13544 KB |
Output is correct |
20 |
Correct |
64 ms |
13256 KB |
Output is correct |
21 |
Correct |
66 ms |
13364 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 |
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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 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 |
0 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
604 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
352 KB |
Output is correct |
6 |
Correct |
0 ms |
352 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 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 |
0 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 |
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 |
352 KB |
Output is correct |
20 |
Correct |
1 ms |
352 KB |
Output is correct |
21 |
Correct |
1 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
352 KB |
Output is correct |
23 |
Correct |
1 ms |
352 KB |
Output is correct |
24 |
Correct |
1 ms |
352 KB |
Output is correct |
25 |
Correct |
94 ms |
12172 KB |
Output is correct |
26 |
Correct |
163 ms |
12180 KB |
Output is correct |
27 |
Correct |
169 ms |
13268 KB |
Output is correct |
28 |
Correct |
168 ms |
13752 KB |
Output is correct |
29 |
Correct |
167 ms |
13516 KB |
Output is correct |
30 |
Correct |
80 ms |
7428 KB |
Output is correct |
31 |
Correct |
79 ms |
13000 KB |
Output is correct |
32 |
Correct |
95 ms |
13520 KB |
Output is correct |
33 |
Correct |
84 ms |
13256 KB |
Output is correct |
34 |
Correct |
103 ms |
13524 KB |
Output is correct |
35 |
Correct |
116 ms |
13264 KB |
Output is correct |
36 |
Correct |
107 ms |
13260 KB |
Output is correct |
37 |
Correct |
121 ms |
11980 KB |
Output is correct |