#include<bits/stdc++.h>
#define oo ll(1e15)
#define maxn 100005
using namespace std;
typedef long long ll;
ll n, k;
vector < pair < ll, ll > > L;
ll a[maxn], b[maxn];
void calc(vector < pair < ll, ll > >& L, ll a[]){
multiset < ll > s, tem;
s.clear(); tem.clear();
ll sum = 0, ret = 0;
a[0] = 0;
for (ll i = 1; i <= n; ++i) {
ll x = L[i - 1].first, y = L[i - 1].second;
sum += x + y;
s.insert(x); ret += x;
s.insert(y); ret += y;
while (s.size() > i) {
ret -= *s.begin();
tem.insert(-(*s.begin()));
s.erase(s.begin());
}
while (-(*tem.begin()) > *s.begin()) {
ret -= *tem.begin() + *s.begin();
ll tg = -*tem.begin();
tem.erase(tem.begin());
tem.insert(-*s.begin());
s.erase(s.begin());
s.insert(tg);
}
a[i] = 2 * ret - sum;
}
}
int main(){
#define TASK "ABC"
// freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout);
ios_base::sync_with_stdio(0);
ll ret = 0;
cin >> k >> n;
for (ll i = 0; i < n; ++i) {
char ch1, ch2;
ll x, y;
cin >> ch1 >> x >> ch2 >> y;
if (x > y) swap(x, y);
if (ch1 == ch2) {
ret += y - x;
continue;
}
L.push_back({x, y});
}
n = L.size();
sort(L.begin(), L.end(), [](pair < ll, ll > i, pair < ll, ll > j){
return i.first + i.second < j.first + j.second;
});
calc(L, a);
reverse(L.begin(), L.end());
calc(L, b);
ll ans = a[n] + ret + n;
if (k == 1)
cout << ans;
else {
for (ll i = 1; i < n; ++i) {
// cerr << i << ' ' << b[4] << '\n';
ans = min(ans, a[i] + b[n - i] + ret + n);
}
cout << ans;
}
return 0;
}
Compilation message
bridge.cpp: In function 'void calc(std::vector<std::pair<long long int, long long int> >&, ll*)':
bridge.cpp:28:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (s.size() > i) {
~~~~~~~~~^~~
# |
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 |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
6 ms |
512 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 |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
132 ms |
13672 KB |
Output is correct |
13 |
Correct |
316 ms |
15312 KB |
Output is correct |
14 |
Correct |
268 ms |
12776 KB |
Output is correct |
15 |
Correct |
146 ms |
9072 KB |
Output is correct |
16 |
Correct |
144 ms |
14568 KB |
Output is correct |
17 |
Correct |
189 ms |
15208 KB |
Output is correct |
18 |
Correct |
126 ms |
14864 KB |
Output is correct |
19 |
Correct |
186 ms |
15208 KB |
Output is correct |
20 |
Correct |
157 ms |
14696 KB |
Output is correct |
21 |
Correct |
219 ms |
14952 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 |
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 |
4 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 |
# |
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 |
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 |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
6 ms |
512 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 |
512 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
6 ms |
512 KB |
Output is correct |
24 |
Correct |
6 ms |
512 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 |
4 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 |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
6 ms |
640 KB |
Output is correct |
14 |
Correct |
6 ms |
640 KB |
Output is correct |
15 |
Correct |
6 ms |
512 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 |
512 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
6 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
6 ms |
512 KB |
Output is correct |
24 |
Correct |
6 ms |
512 KB |
Output is correct |
25 |
Correct |
139 ms |
13672 KB |
Output is correct |
26 |
Correct |
238 ms |
13804 KB |
Output is correct |
27 |
Correct |
321 ms |
14828 KB |
Output is correct |
28 |
Correct |
387 ms |
15212 KB |
Output is correct |
29 |
Correct |
367 ms |
15180 KB |
Output is correct |
30 |
Correct |
126 ms |
8176 KB |
Output is correct |
31 |
Correct |
141 ms |
14564 KB |
Output is correct |
32 |
Correct |
193 ms |
15204 KB |
Output is correct |
33 |
Correct |
124 ms |
14824 KB |
Output is correct |
34 |
Correct |
196 ms |
15208 KB |
Output is correct |
35 |
Correct |
155 ms |
14824 KB |
Output is correct |
36 |
Correct |
194 ms |
15080 KB |
Output is correct |
37 |
Correct |
136 ms |
13672 KB |
Output is correct |