Source Code

#include <iostream>

using std::swap;
using std::cout;
using std::cin;
using std::endl;


int main()
{


    int n, k;
    cin >> n >> k;
    int a[n];

    for(int i=0; i<n; i++){
        cin >> a[i];
    }

    int smallest;
    bool Flag = false;

    for(int i=0; i<n-1; i++){
        smallest = i;
        for (int u=i+1; u<n; u++){
            if (a[smallest] > a[u]){
                smallest = u;
                Flag = true;
            }
        }
        if(Flag == false) break;
        swap(a[smallest], a[i]);
    }

    int numOf0 = 0; 
    for(int i=0; i<k; i++){
        if(a[i]==0) numOf0++;
    }

    int sums = 0;
    int tk = k;
    while(tk != 1 && numOf0 > 0){
        for(int i=0; i<n; i++){
            if(a[i]+1==a[i+1] || a[i] == a[i+1]) continue;
            sums += a[i-1]+1;
        } tk--;
    }

    if(numOf0 <= 0) sums = 0;

    cout << sums;

    return 0;
}
Copy
Bitar The Handy Man Abd20200355
GNU G++17
3 ms
652 KB
Wrong Answer