Submission #159670

# Submission time Handle Problem Language Result Execution time Memory
159670 2019-10-23T20:46:17 Z theknife2001 Simfonija (COCI19_simfonija) C++17
0 / 110
302 ms 3220 KB
#include <bits/stdc++.h>
#define ll long long

using namespace std;
const int N=1e5+55;
int a[N];
int b[N];
ll x[N];
int n,k;
int pre;
int suf;
ll cur;
int j,i;

void solve(int pos)
{
	pre--;
	suf++;
	//cout<<pre<<' '<<suf<<endl;
	while(x[j]-x[pos]<x[pos]-x[i]&&i<pos&&j<n)
	{
		//cout<<' '<<i<<' '<<j<<endl;
		cur+=x[j]-x[pos-1];
		pre++;
		suf--;
		cur-=x[pos-1]-x[i];
		j--;
		i++;
	}
	//cout<<cur<<' ';
	ll dif=x[pos]-x[pos-1];
	cur+=dif*suf-dif*(pre+1);
	cout<<cur<<endl;
}
		
		

int main()
{
  	ios::sync_with_stdio(false);
	cin>>n>>k;
	for(int i=0;i<n;i++)
	cin>>a[i];
	for(int i=0;i<n;i++)
	cin>>b[i];
	for(int i=0;i<n;i++)
	x[i]=a[i]-b[i];
	sort(x,x+n);
	int temp=x[0];
	for(int i=0;i<n;i++)
	{
		x[i]-=temp;
		if(i<n-k)
		cur+=abs(x[i]);
	}
	j=n-k;
	ll ans=cur;
	pre=n-k-1;
	for(int i=1;i<n;i++)
	{
		solve(i);
		ans=min(ans,cur);
	}
	cout<<ans<<endl;
	return 0;
}

# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 299 ms 3220 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 297 ms 3136 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 298 ms 3064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 287 ms 2836 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 302 ms 3132 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 291 ms 2780 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 299 ms 3196 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 298 ms 3064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 295 ms 3144 KB Output isn't correct
2 Halted 0 ms 0 KB -