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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>

class List
{
int count;
public:
int* Items;
const int &Count;
List(int n = 0) : count(n), Count(count)
{
Items = new int[count];
}

~List()
{
delete Items;
}
void Add(int item)
{
int* temp = new int[count];
for (int i=0; i<count; i++)
temp[i] = Items[i];
count++;
delete Items;
Items = new int[count];
for (int i=0; i<count-1; i++)
Items[i] = temp[i];
delete temp;
Items[count-1] = item;
}
bool Contains(int item)
{
for (int i=0; i<count; i++)
if (Items[i] == item)
return true;
return false;
}
int IndexOf(int item)
{
for (int i=0; i<count; i++)
if (Items[i] == item)
return i;
return -1;
}
List* SubList(int size)
{
List* ret = new List();
for (int i=0; i<size; i++)
ret->Add(Items[i]);
return ret;
}
int operator[](int ind)
{
if (ind > count)
;// throw...
return Items[ind];
}
void operator<<(int item)
{
Add(item);
}
List operator=(List list)
{
List ret;
for (int i=0; i<list.Count; i++)
ret << list[i];
return ret;
}
int Differents()
{
int diffs = 0;
List temp;
temp << Items[0];
for (int i=1; i<count; i++)
{
if (!temp.Contains(Items[i]))
diffs;
temp << Items[i];
}
return diffs;
}
int FirstSame()
{
List temp;
temp << Items[0];
for (int i=1; i<count; i++)
{
if (temp.Contains(Items[i]))
return i;
temp << Items[i];
}
return 0;
}
void Swap(int i, int j)
{
int temp = Items[j];
Items[j] = Items[i];
Items[i] = temp;
}
};

int main()
{
int n, k, inp, diffs = 0;
std::cin >> n >> k;
if (k > n)
return -1;
List tms;
for (int i=0; i<n; i++)
{
std::cin >> inp;
if (!tms.Contains(inp))
diffs++;
tms << inp;
}
if (diffs < k)
{
std::cout << -1;
return 0;
}
int cur, steps = 0;
List* left;
while (1)
{
left = tms.SubList(k);
cur = tms.FirstSame();
if (cur >= k)
{
delete left;
std::cout << steps;
return 0;
}
while (left->Contains(tms[cur+1]))
{
tms.Swap(cur++, cur);
steps++;
if (cur == k)
left = tms.SubList(k);
}
tms.Swap(cur, cur+1);
while (cur > tms.FirstSame())
{
tms.Swap(cur--, cur);
steps++;
}
}
delete left;
return 0;
}