#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, k;
ll base, ans;
ll a[100002], b[100002];
int main(){
scanf("%d %d", &k, &n);
for(int i=1; i<=n; i++){
char C1, C2;
int X1, X2;
scanf(" %c %d %c %d", &C1, &X1, &C2, &X2);
if(C1==C2) base += abs(X1-X2), n--, i--;
else a[i] = X1, b[i] = X2;
}
if(n==0){
printf("%lld", base);
return 0;
}
if(k==1){
vector<ll> vec;
for(int i=1; i<=n; i++) vec.push_back(a[i]), vec.push_back(b[i]);
sort(vec.begin(), vec.end());
ans = 1e18;
ll lsum = 0, lcnt = 0, rsum = accumulate(vec.begin(), vec.end(), 0LL), rcnt = (int)vec.size();
for(int i=0; i<n*2; i++){
rsum -= vec[i], rcnt--;
ans = min(ans, (lcnt * vec[i] - lsum) + (rsum - rcnt * vec[i]) + n);
lsum += vec[i], lcnt++;
}
printf("%lld", ans+base);
return 0;
}
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:12:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | scanf("%d %d", &k, &n);
| ~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:16:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | scanf(" %c %d %c %d", &C1, &X1, &C2, &X2);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
31 ms |
4800 KB |
Output is correct |
13 |
Correct |
51 ms |
6312 KB |
Output is correct |
14 |
Correct |
39 ms |
5016 KB |
Output is correct |
15 |
Correct |
29 ms |
3704 KB |
Output is correct |
16 |
Correct |
36 ms |
5668 KB |
Output is correct |
17 |
Correct |
48 ms |
6316 KB |
Output is correct |
18 |
Correct |
58 ms |
5964 KB |
Output is correct |
19 |
Correct |
62 ms |
6312 KB |
Output is correct |
20 |
Correct |
39 ms |
5836 KB |
Output is correct |
21 |
Correct |
47 ms |
6064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
308 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
304 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |