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
#include<cstdio>
#include<vector>

using namespace std;

int main() {

    int bottlesNum;
    int requiredFromLeft;

    scanf("%d %d\n", &bottlesNum, &requiredFromLeft);
    
    vector<int> brands(bottlesNum, 0);
    for (int i = 0; i < bottlesNum; i++) {
        scanf("%d", &brands[i]);
    }

    vector<bool> included(bottlesNum, false);
    int nextIndex = 0;
    long long movesNum = 0;
    for (int i = 0; i < bottlesNum && nextIndex < requiredFromLeft; i++) {
        int brand = brands[i];
        if (!included[brand - 1]) {
            included[brand - 1] = true;
            movesNum += i - nextIndex;
            nextIndex++;
        }
    }

    if (nextIndex < requiredFromLeft) {
        printf("-1\n");
    } else {
        printf("%lld\n", movesNum);
    }

    return 0;
}