1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <algorithm>
#define MAX 1000009

using namespace std;

int values[MAX], used[MAX];

int main()
{
    int n, k;
    
    cin >> n >> k;
    
    for (int i=0; i<n; i++) cin >> values[i];

    
    int result = 0, doubledBottles = 0;
    for (int i=0; i<n && k; i++) {
        if (used[values[i]]) {
            doubledBottles++;
        } else {
            result += doubledBottles;
            used[values[i]] += 1;
            k -= 1;
        }
    }
    
    if (k) cout << -1;
    else cout << result;
}