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

constexpr int N = 5 * 100 * 1000 + 1;

bool bottles[N];

int main() {
    long long n, k, bottle, to_move = 0, correct_left = 0, result = 0;
    scanf("%lld%lld", &n, &k);
    for (int i = 0; i < n; ++i) {
        scanf("%lld", &bottle);
        if (bottles[bottle]) {
            to_move++;
        } else {
            bottles[bottle] = true;
            result += to_move;
            correct_left++;
            if (correct_left == k) {
                printf("%lld\n", result);
                return 0;
            }
        }
    }
    printf("-1\n");
    return 0;
}