#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
const int nmax=100005;
struct thing
{
int ss,x,y;
}v[nmax];
long long p[nmax],s[nmax];
long long ans,fx;
int norm[2*nmax];
char wh1,wh2;
int n,i,j,nrr,cate,k,p1,p2;
bool comp(thing unu,thing doi)
{
return (unu.ss<doi.ss);
}
struct aibul_smecher
{
long long aib[2*nmax];
inline int lbit(int x)
{
return ((x^(x-1))&x);
}
void update(int poz,long long val)
{
for(int idx=poz;idx<=2*n;idx+=lbit(idx))
aib[idx]+=val;
}
long long compute(int poz)
{
long long ret=0;
for(int idx=poz;idx>0;idx-=lbit(idx))
ret+=aib[idx];
return ret; //:*
}
int find_kth(int k)
{
int ret=0,act=0;
for(int p=17;p>=0;p--)
if(ret+(1<<p)<=2*n&&act+aib[ret+(1<<p)]<k)
{
ret+=(1<<p);
act+=aib[ret];
}
ret++;
return ret;
}
void clr()
{
for(int idx=1;idx<=2*n;idx++)
aib[idx]=0;
}
}nr,sum;
int cb(int val)
{
int ret=0;
for(int p=17;p>=0;p--)
if(ret+(1<<p)<=nrr&&norm[ret+(1<<p)]<=val)
ret+=(1<<p);
return ret;
}
void ins(int val)
{
int poz=cb(val);
nr.update(poz,1);
sum.update(poz,val);
}
long long calc(long long x)
{
int poz=nr.find_kth(x);
long long act=norm[poz];
return (act*nr.compute(poz)-2*sum.compute(poz)+1LL*sum.compute(2*n)-act*(nr.compute(2*n)-nr.compute(poz)));
}
void solve()
{
for(i=1;i<=cate;i++)
{
ins(v[i].x);
ins(v[i].y);
p[i]=calc(i);
}
sum.clr();nr.clr();
ans=p[cate];
for(i=cate;i>=1;i--)
{
ins(v[i].x);
ins(v[i].y);
s[i]=calc((cate-i+1));
if(p[i-1]+s[i]<ans)
ans=1LL*p[i-1]+s[i];
}
}
int main()
{
//freopen("data.in","r",stdin);
ios_base::sync_with_stdio(false);
cin>>k>>n;
for(i=1;i<=n;i++)
{
cin>>wh1>>p1>>wh2>>p2;
if(p1>p2) swap(p1,p2);
if(wh1!=wh2)
{
v[++cate]={p1+p2,p1,p2};
norm[++nrr]=p1;norm[++nrr]=p2;
}
else fx+=(1LL*(p2-p1));
}
sort(v+1,v+cate+1,comp);
sort(norm+1,norm+nrr+1);
solve();
if(k==1)
ans=p[cate];
ans+=1LL*cate;
cout<<ans+fx;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
424 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
69 ms |
7800 KB |
Output is correct |
13 |
Correct |
185 ms |
9336 KB |
Output is correct |
14 |
Correct |
160 ms |
7544 KB |
Output is correct |
15 |
Correct |
96 ms |
5752 KB |
Output is correct |
16 |
Correct |
76 ms |
8708 KB |
Output is correct |
17 |
Correct |
102 ms |
9340 KB |
Output is correct |
18 |
Correct |
189 ms |
9052 KB |
Output is correct |
19 |
Correct |
133 ms |
9368 KB |
Output is correct |
20 |
Correct |
77 ms |
8876 KB |
Output is correct |
21 |
Correct |
116 ms |
9036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
352 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
3 ms |
504 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
3 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
3 ms |
504 KB |
Output is correct |
23 |
Correct |
2 ms |
504 KB |
Output is correct |
24 |
Correct |
3 ms |
504 KB |
Output is correct |
25 |
Correct |
70 ms |
7772 KB |
Output is correct |
26 |
Correct |
123 ms |
7928 KB |
Output is correct |
27 |
Correct |
190 ms |
8932 KB |
Output is correct |
28 |
Correct |
194 ms |
9436 KB |
Output is correct |
29 |
Correct |
195 ms |
9416 KB |
Output is correct |
30 |
Correct |
87 ms |
5112 KB |
Output is correct |
31 |
Correct |
75 ms |
8696 KB |
Output is correct |
32 |
Correct |
105 ms |
9336 KB |
Output is correct |
33 |
Correct |
109 ms |
9004 KB |
Output is correct |
34 |
Correct |
120 ms |
9376 KB |
Output is correct |
35 |
Correct |
81 ms |
8952 KB |
Output is correct |
36 |
Correct |
114 ms |
9052 KB |
Output is correct |
37 |
Correct |
54 ms |
8184 KB |
Output is correct |