Submission #554752

# Submission time Handle Problem Language Result Execution time Memory
554752 2022-04-29T10:58:56 Z A_D Simfonija (COCI19_simfonija) C++14
110 / 110
29 ms 3300 KB
#include <bits/stdc++.h>
#define int long long
#define ii pair<int,int>
#define F first
#define S second
using namespace std;

const int N=1e5+100;
int a[N];
int pre[N];

void solve()
{
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=n;i++){
        int b;
        cin>>b;
        a[i]-=b;
    }
    sort(a+1,a+n+1);
    for(int i=2;i<=n;i++){
        a[i]-=a[1];
    }
    a[1]=0;
    for(int i=1;i<=n;i++){
        pre[i]=pre[i-1]+a[i];
    }
    int l=k+1,r=n;
    int ans=1e18;
    /*
    for(int i=1;i<=n;i++){
        cout<<a[i]<<" ";
    }
    cout<<"\n";
    */
    while(l>=1){
        int mid=(l+r)/2;
        int v=(a[mid]*(mid-l+1)-(pre[mid]-pre[l-1]))+((pre[r]-pre[mid])-(a[mid]*(r-mid)));
        ans=min(ans,v);

      //  cout<<l<<" "<<r<<" "<<v<<"\n";


        l--;
        r--;
    }
    cout<<ans<<"\n";
}

main()
{
///*
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    //*/
    int t=1;
//    cin>>t;
    while(t--){
        solve();
    }
}






/*

7

7 2 5 4 7 4 2



*/










Compilation message

simfonija.cpp:54:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   54 | main()
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 3272 KB Output is correct
2 Correct 24 ms 3256 KB Output is correct
3 Correct 22 ms 3200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 3276 KB Output is correct
2 Correct 24 ms 3292 KB Output is correct
3 Correct 22 ms 3272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 3276 KB Output is correct
2 Correct 22 ms 3276 KB Output is correct
3 Correct 20 ms 3296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 2628 KB Output is correct
2 Correct 22 ms 3260 KB Output is correct
3 Correct 26 ms 3276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 3276 KB Output is correct
2 Correct 25 ms 3256 KB Output is correct
3 Correct 25 ms 3276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 3284 KB Output is correct
2 Correct 23 ms 3264 KB Output is correct
3 Correct 24 ms 3272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 3272 KB Output is correct
2 Correct 29 ms 3264 KB Output is correct
3 Correct 26 ms 3256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 3256 KB Output is correct
2 Correct 28 ms 3276 KB Output is correct
3 Correct 26 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 3300 KB Output is correct
2 Correct 22 ms 3276 KB Output is correct
3 Correct 27 ms 3260 KB Output is correct