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
/*
    Piotr Bieńkowski
    bbienkos@gmail.com
    Akademickie Liceum Ogólnokształcące Politechniki Wrocławskiej
*/

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define debug(x); cerr << #x << " = " << x << "\n";
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

#define int long long

const int N = 5e5 + 7;
const int INF = 1e9 + 9;

int tab[N];
bool is[N];
bool now[N];
int n, k;


int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> k;
    tab[0] = -INF;
    int cnt = 0;
    for(int i = 1; i <= n; i++)
    {
        cin >> tab[i];
        cnt += !is[tab[i]];
        is[tab[i]] = true;
    }

    if(cnt < k)
    {
        cout << "-1\n";
        return 0;
    }

    now[tab[1]] = true;
    int wsk = 2, cost = 0;
    for(int i = 2; i <= k; i++)
    {
        while(now[tab[wsk]] && wsk <= n) wsk++;
        cost += (wsk - i);
        now[tab[wsk]] = true;
        wsk ++;
    }
    cout << cost << "\n";
}