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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int num_of_bottles, num_of_places;
    cin >> num_of_bottles >> num_of_places;
    vector<int> bottles = {};
    int diffrent_kinds = 0;
    for (int i = 0; i < num_of_bottles; i++)
    {
        int kind;
        cin >> kind;
        bool found = (find(bottles.begin(), bottles.end(), kind) != bottles.end());
        if (found == false)
            diffrent_kinds++;
        bottles.push_back(kind);
    }
    bottles.shrink_to_fit();
    if (diffrent_kinds < num_of_places)
    {
        cout << -1;
    }
    else
    {
        vector<int> already_sorted = {};
        already_sorted.push_back(bottles[0]);
        int seconds = 0;
        int already_sorted_places = 0;
        for (int i = 1; i < num_of_places; i++)
        {
            for (int j = max(i, already_sorted_places); j < num_of_bottles; j++)
            {
                if (find(already_sorted.begin(), already_sorted.end(), bottles[j]) != already_sorted.end())
                    continue;
                else
                {
                    already_sorted.push_back(bottles[j]);
                    seconds += (j - i);
                    already_sorted_places = j;
                    break;
                }
            }
        }
        cout << seconds;
    }
    return 0;
}