Submission #100268

# Submission time Handle Problem Language Result Execution time Memory
100268 2019-03-10T07:18:07 Z ami Simfonija (COCI19_simfonija) C++14
110 / 110
49 ms 4344 KB
#include <bits/stdc++.h>
#define sz(c)      int(c.size())
#define rep(i,a,b) for (int i=a; i<(b); ++i)
#define per(i,a,b) for (int i=(b)-1; i>=(a); --i)
using namespace std;
using ll = long long;

ll const INF=1e18;
int const MAXN=110000;
int N,K;
int A[MAXN];
int B[MAXN];
ll d[MAXN];
ll prSum[MAXN];

ll sum(int l,int r) {
	return prSum[r]-prSum[l];
}

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	cout<<fixed<<setprecision(10);

	cin>>N>>K;
	rep(i,0,N) cin>>A[i];
	rep(i,0,N) cin>>B[i];

	rep(i,0,N) d[i]=A[i]-B[i];
	sort(d,d+N);

	prSum[0]=0;
	rep(i,0,N) prSum[i+1]=prSum[i]+d[i];

	int n=N-K;
	ll res=INF;
	rep(i,n/2,N-(n-1)/2) {
		ll leftSum=d[i]*(n/2) - sum(i-n/2,i);
		ll rightSum=sum(i+1,(i+1)+(n-1)/2) - d[i]*((n-1)/2);
		res=min(res,leftSum+rightSum);
	}
	cout<<res<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 4224 KB Output is correct
2 Correct 31 ms 4216 KB Output is correct
3 Correct 31 ms 4344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 4216 KB Output is correct
2 Correct 33 ms 4216 KB Output is correct
3 Correct 31 ms 4216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 4088 KB Output is correct
2 Correct 46 ms 4192 KB Output is correct
3 Correct 31 ms 4216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 3448 KB Output is correct
2 Correct 30 ms 4088 KB Output is correct
3 Correct 29 ms 4344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 4088 KB Output is correct
2 Correct 38 ms 4172 KB Output is correct
3 Correct 49 ms 4088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 4144 KB Output is correct
2 Correct 37 ms 4216 KB Output is correct
3 Correct 32 ms 4188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 4088 KB Output is correct
2 Correct 35 ms 4088 KB Output is correct
3 Correct 33 ms 4088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 4188 KB Output is correct
2 Correct 33 ms 4216 KB Output is correct
3 Correct 35 ms 4088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 4088 KB Output is correct
2 Correct 44 ms 4088 KB Output is correct
3 Correct 34 ms 4260 KB Output is correct