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
#define _CRT_SECURE_NO_WARNINGS 1
#define _WINSOCK_DEPRECATED_NO_WARNINGS 1

#include <iostream>
#include <string>
using namespace std;

int buteleczki[500235];
int   unikalne[500216];
int main()
{
    int n, k;
    scanf("%d%d", &n, &k);


    for(int i = 0; i < n; ++i)
    {
        int p;
        scanf("%d", &p);
        buteleczki[i] = p;       
    }

    int unikaty = 0;
    int j = 0;
    long long wynik = 0;
    for (int i = 0; i < n; ++i)
    {
        int producent = buteleczki[i];
        if (unikalne[producent] == 0)
        {
            unikaty++;
            unikalne[producent] = 1;
        }
        else
        {
            for (; j < n; ++j)
            {
                int producentJ = buteleczki[j];
                if (unikalne[producentJ] == 0)
                {
                    unikaty++;
                    unikalne[producentJ] = 1;
                    wynik += j - i;
                    break;
                }
               
            }
        }
        if (unikaty >= k)
        {
            break;
        }
    }

    if (unikaty < k)
    {
        printf("-1");
    }
    else
    {
        printf("%lld", wynik);
    }
}