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
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool used[500005] = {false};

int main() {
    int numBottles, seqLen, numDistinct = 0;
    unsigned long long repeated = 0, totalJumps = 0;
    
    ios::sync_with_stdio(false);

    cin >> numBottles >> seqLen;
    vector<int> bottles(numBottles);

    for(int i=0; i<numBottles; ++i) {
        cin >> bottles[i];

        if(used[bottles[i]]) {
            ++repeated;
        }
        else {
            used[bottles[i]] = true;
            totalJumps += repeated;
            ++numDistinct;
        }

        if(numDistinct == seqLen)
            break;
    }

    if(numDistinct == seqLen)
        cout << totalJumps << endl;    
    else
        cout << "-1" << endl;

    return 0;
}