#include <bits/stdc++.h>
#define int long long
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define l(a) a.second.first
#define r(a) a.second.second
#define v(a) a.first
using namespace std;
signed main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n,k; cin >> k >> n;
int sum=0;
vector<pair<int,pair<int,int>>> a;
for (int i = 0; i < n; i++){
char p,q; int s,t; cin >> p >> s >> q >> t;
if(s>t) swap(s,t);
if(p!=q) {
a.push_back({s+t,{2*s,2*t}});
sum++;
}
sum+=abs(s-t);
}
n=sz(a);
sort(all(a));
int mn=1e18;
if(k==1){
int md=(n-1)/2;
mn=0;
for (int j = 0; j < n; j++)
{
mn+=max(0LL,max(l(a[j])-v(a[md]),v(a[md])-r(a[j])));
}
}else{
for (int i = 0; i <= n; i++)
{
int sm=0;
int md=(i-1)/2;
for (int j = 0; j < i; j++)
{
sm+=max(0LL,max(l(a[j])-v(a[md]),v(a[md])-r(a[j])));
}
md=(i+n-1)/2;
for (int j = 0; j < i; j++)
{
sm+=max(0LL,max(l(a[j])-v(a[md]),v(a[md])-r(a[j])));
}
mn=min(mn,sm);
}
}
cout << sum+mn << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |