#include<bits/stdc++.h>
#define int long long
using namespace std;
char c, d;
int a, b, n, i, j, k;
long long ans, ans2=1e18, A[100010], B[100010], sum1, sum2;
struct st
{
int a, b;
};
vector<st>v;vector<int>v1;
bool cmp(st a, st b)
{
return a.a+a.b<b.a+b.b;
}
priority_queue<int>pq2;
priority_queue<int, vector<int>, greater<int> >pq1;
main()
{
ios_base::sync_with_stdio(!cin.tie(NULL));
for(cin>>k>>n;i++<n;)
{
cin>>c>>a>>d>>b;
if(c==d)ans+=abs(b-a);
else if(k==2)ans++,v.push_back({a, b});
else ans++,v1.push_back(a),v1.push_back(b);
}
if(k==1)
{
sort(v1.begin(), v1.end());
for(i=0;i<v1.size()/2;i++)ans-=v1[i];
for(i=v1.size()/2;i<v1.size();i++)ans+=v1[i];
cout<<ans;return 0;
}
sort(v.begin(), v.end(), cmp);
for(i=0;i<v.size();i++)
{
if(v[i].a>v[i].b)swap(v[i].a, v[i].b);
if(pq1.size()==0)
{
pq1.push(v[i].b);sum1+=v[i].b;
pq2.push(v[i].a);sum2+=v[i].a;
}
else
{
if(v[i].b<=pq2.top())
{
a=pq2.top();
pq1.push(a);sum1+=a;
pq2.pop();sum2-=a;
pq2.push(v[i].a);sum2+=v[i].a;
pq2.push(v[i].b);sum2+=v[i].b;
}
else if(v[i].a<=pq1.top())
{
pq2.push(v[i].a);sum2+=v[i].a;
pq1.push(v[i].b);sum1+=v[i].b;
}
else
{
a=pq1.top();
pq2.push(a);sum2+=a;
pq1.pop();sum1-=a;
pq1.push(v[i].a);sum1+=v[i].a;
pq1.push(v[i].b);sum1+=v[i].b;
}
}
A[i+1]=sum1-sum2;
}
sum1=sum2=0;while(pq1.size())pq1.pop();while(pq2.size())pq2.pop();
i=v.size();
for(i--;i>=0;i--)
{
if(pq1.size()==0)
{
pq1.push(v[i].b);sum1+=v[i].b;
pq2.push(v[i].a);sum2+=v[i].a;
}
else
{
if(v[i].b<=pq2.top())
{
a=pq2.top();
pq1.push(a);sum1+=a;
pq2.pop();sum2-=a;
pq2.push(v[i].a);sum2+=v[i].a;
pq2.push(v[i].b);sum2+=v[i].b;
}
else if(v[i].a<=pq1.top())
{
pq2.push(v[i].a);sum2+=v[i].a;
pq1.push(v[i].b);sum1+=v[i].b;
}
else
{
a=pq1.top();
pq2.push(a);sum2+=a;
pq1.pop();sum1-=a;
pq1.push(v[i].a);sum1+=v[i].a;
pq1.push(v[i].b);sum1+=v[i].b;
}
}
B[i]=sum1-sum2;
}
for(i=0;i<=v.size();i++)
{
ans2=min(ans2, A[i]+B[i]);
}
cout<<ans+ans2;
}
Compilation message
bridge.cpp:18:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
18 | main()
| ^
bridge.cpp: In function 'int main()':
bridge.cpp:31:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(i=0;i<v1.size()/2;i++)ans-=v1[i];
| ~^~~~~~~~~~~~
bridge.cpp:32:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(i=v1.size()/2;i<v1.size();i++)ans+=v1[i];
| ~^~~~~~~~~~
bridge.cpp:36:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for(i=0;i<v.size();i++)
| ~^~~~~~~~~
bridge.cpp:105:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<st>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for(i=0;i<=v.size();i++)
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
23 ms |
2460 KB |
Output is correct |
13 |
Correct |
44 ms |
2484 KB |
Output is correct |
14 |
Correct |
36 ms |
2436 KB |
Output is correct |
15 |
Correct |
26 ms |
1484 KB |
Output is correct |
16 |
Correct |
39 ms |
2512 KB |
Output is correct |
17 |
Correct |
33 ms |
2496 KB |
Output is correct |
18 |
Correct |
39 ms |
2504 KB |
Output is correct |
19 |
Correct |
45 ms |
2432 KB |
Output is correct |
20 |
Correct |
31 ms |
2524 KB |
Output is correct |
21 |
Correct |
40 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
224 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
208 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
49 ms |
6116 KB |
Output is correct |
26 |
Correct |
69 ms |
7272 KB |
Output is correct |
27 |
Correct |
86 ms |
8040 KB |
Output is correct |
28 |
Correct |
92 ms |
8656 KB |
Output is correct |
29 |
Correct |
91 ms |
8704 KB |
Output is correct |
30 |
Correct |
51 ms |
4412 KB |
Output is correct |
31 |
Correct |
42 ms |
8040 KB |
Output is correct |
32 |
Correct |
93 ms |
8616 KB |
Output is correct |
33 |
Correct |
56 ms |
8244 KB |
Output is correct |
34 |
Correct |
81 ms |
8708 KB |
Output is correct |
35 |
Correct |
59 ms |
8268 KB |
Output is correct |
36 |
Correct |
88 ms |
8364 KB |
Output is correct |
37 |
Correct |
39 ms |
7056 KB |
Output is correct |