CSES - Datatähti Open 2021 - Results
Submission details
Task:Programmers
Sender:koosaga
Submission time:2021-01-30 10:04:47 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:19:5: error: 'sort' was not declared in this scope
     sort(a,a+n);
     ^~~~
input/code.cpp:19:5: note: suggested alternative: 'qsort'
     sort(a,a+n);
     ^~~~
     qsort
input/code.cpp:15:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&n,&k);
     ~~~~~^~~~~~~~~~~~~~~
input/code.cpp:17:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&a[i]);
         ~~~~~^~~~~~~~~~~~

Code

#include <cstdio>
#include <map>
#include <queue>
#include <utility>
using namespace std;
typedef pair<int,int> pi;
typedef map<int,bool> mp;

priority_queue<pi,vector<pi>,greater<pi> > pq;
mp viable;

int n,k,a[500005];

int main(){
    scanf("%d %d",&n,&k);
    for (int i=0; i<n; i++) {
        scanf("%d",&a[i]);
    }
    sort(a,a+n);
    for (int i=n-1; i; i--) {
        a[i] -= a[i-1];
        pq.push(pi(a[i],i));
        viable[i] = 1;
    }
    int res = 0;
    for (int i=0; i<k; i++) {
        int p = pq.top().second;
        pq.pop();
        if(viable.count(p) == 0){
            i--;
            continue;
        }
        res += a[p];
        viable.erase(viable.find(p));
        mp::iterator it = viable.upper_bound(p);
        int x = 0, cont = 0;
        if(it != viable.end()){
            x += a[(*it).first];
            viable.erase((*it).first);
        }
        else cont = 1;
        it = viable.upper_bound(p);
        if(it != viable.begin()){
            it--;
            x += a[(*it).first];
            viable.erase((*it).first);
        }
        else cont = 1;
        if(cont) continue;
        viable[p] = 1;
        a[p] = x - a[p];
        pq.push(pi(a[p],p));
    }
    printf("%d",res);
}