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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;
int n,k,a,q,z,wynik;

bool in(int m[],int i,int a){
    bool tru=0;
    int j=0;
    while (tru==0 && j<=i){
    if  (m[j]==a)  
        {tru=1;}
    j++;
    }
    return tru;
}

int main()
{
    cin>>n;
    cin>>k;
    bool exists;
    wynik=0;
    q=0;
    int count=0;
    int swap;
    int unique[n];
    int uni[n];
    for (int i=0;i<n;i++)
        {
            uni[i]=0;
        }
    int t[n];
    for (int i=0;i<n;i++)
       {
            cin>>a;
            t[i]=a;
            if(in(uni,n,t[i])==false)
            {
              uni[q]=t[i];
              q++;
            }
       }
    if(q>=k)
      {q=0;
        for (int i=0;i<k;i++)
        {
            if(in(unique,n,t[i])==false)
            {
              unique[q]=t[i];
              q++;
            }
            else
            {
                while(in(unique,n,t[i])==true)
                    { 
                    z=i;
                    while(in(unique,n,t[z])==true)
                        {z++;}
                    swap=t[z];
                    t[z]=t[z-1];
                    t[z-1]=swap;
                    wynik++;
                    }
            unique[q]=t[i];
            q++;
            }
        }}
    else
       {wynik=-1;}
    cout<<wynik;
    return 0;
}