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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<vector>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    long long n;
    long long k;
    cin>>n>>k;

    vector<long long> bottles(n+1);

    for(long long i=1;i<=n;i++)
    {
        cin>>bottles[i];
    }

    long long changes_counter=0;
    long long diffrent_bottles=0;

    vector<int> already_seen(n+1,0);

    for(long long x=1;x<=n;x++)
    {

        if(diffrent_bottles==k)
        {
            break;
        }

        if(already_seen[bottles[x]]==0)
        {
            already_seen[bottles[x]]=1;

            changes_counter+=x-diffrent_bottles-1;

            diffrent_bottles++;
        }
    }

    if(diffrent_bottles<k)
    {
        cout<<"-1"<<'\n';
    }
    else
    {
        cout<<changes_counter<<'\n';
    }

    return 0;
}