#include "bits/stdc++.h"
#define ll long long int
#define pb push_back
#define pii pair<int,int>
#define ff first
#define ss second
#define sz size()
const int N = 2e5 + 1;
using namespace std;
int n, k, a[N], b[N];
ll ans, p[N];
char s[N], d[N];
vector <ll> v;
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> k >> n;
v.pb(0);
for(int i = 1; i <= n; i++){
cin >> s[i] >> a[i] >> d[i] >> b[i];
if(s[i] == d[i]) ans += abs(a[i] - b[i]);
else{
v.pb(a[i]);
v.pb(b[i]);
}
}
sort(v.begin(), v.end());
int n1 = v.sz-1;
for(int i = 1; i <= n1; i++) v[i] += v[i-1];
ans += n1 / 2;
ll mn = 1e15;
if (n1 == 0) mn = 0;
if(k == 1){
for(int i = 1; i <= n1; i++){
int nd = v[i] - v[i-1];
ll san = (i*nd)-v[i] + (v.back() - v[i-1]) - ((n1-i+1)*nd);
mn = min(mn,san);
}
cout << ans + mn << '\n';
}
else{
for(int i = 1; i <= n1; i++){
for(int j = i+1; j <= n1; j++){
int nd = v[i] - v[i-1], nd1 = v[j] - v[j-1];
ll san = 0;
for(int u = 1; u <= n; u++){
if(s[u] == d[u]) continue;
san += min(abs(a[u]-nd)+abs(b[u]-nd),abs(a[u]-nd1)+abs(b[u]-nd1));
}
mn = min(mn,san);
}
}
cout << ans + mn << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
4 ms |
2396 KB |
Output is correct |
4 |
Correct |
4 ms |
2440 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
5 ms |
2524 KB |
Output is correct |
8 |
Correct |
5 ms |
2396 KB |
Output is correct |
9 |
Correct |
4 ms |
2520 KB |
Output is correct |
10 |
Correct |
5 ms |
2396 KB |
Output is correct |
11 |
Correct |
5 ms |
2396 KB |
Output is correct |
12 |
Correct |
5 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
3 ms |
2396 KB |
Output is correct |
4 |
Correct |
3 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
4 ms |
2528 KB |
Output is correct |
8 |
Correct |
5 ms |
2648 KB |
Output is correct |
9 |
Correct |
4 ms |
2392 KB |
Output is correct |
10 |
Correct |
7 ms |
2392 KB |
Output is correct |
11 |
Correct |
5 ms |
2396 KB |
Output is correct |
12 |
Correct |
5 ms |
2396 KB |
Output is correct |
13 |
Execution timed out |
2096 ms |
2396 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
4 ms |
2396 KB |
Output is correct |
4 |
Correct |
4 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
5 ms |
2396 KB |
Output is correct |
8 |
Correct |
5 ms |
2396 KB |
Output is correct |
9 |
Correct |
4 ms |
2396 KB |
Output is correct |
10 |
Correct |
5 ms |
2392 KB |
Output is correct |
11 |
Correct |
4 ms |
2396 KB |
Output is correct |
12 |
Correct |
7 ms |
2396 KB |
Output is correct |
13 |
Execution timed out |
2033 ms |
2392 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |