/*#pragma GCC target ("avx2") #pragma GCC optimize ("O3") #pragma GCC optimize ("unroll-loops")*/ #include <bits/stdc++.h> #define bit(n, i) (((n)>>(i))&1) #define cat(x) cout << #x << " = " << x << "\n" #define pb push_back #define mp make_pair #define se second #define fi first #define inf 2000000000 #define llinf 2000000000000000000LL #define forn(i, a, b) for(ll i = (a); i <= (b); ++i) #define all(x) (x).begin(), (x).end() #define ppc(x) __builtin_popcount(x) using namespace std; typedef long long ll; typedef double ld; typedef pair<int, int> i2; typedef vector<int> vi; typedef vector<ll> vll; const int N = 5e5+9; bool vis[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; int dcnt = 0; ll res = 0; bool ok = false; forn(i, 1, n) { int a; cin >> a; if(!vis[a]) { vis[a] = true; dcnt++; res += i-dcnt; if(dcnt == k) { ok = true; break; } } } cout << (ok ? res : -1); return 0; }
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 | /*#pragma GCC target ("avx2") #pragma GCC optimize ("O3") #pragma GCC optimize ("unroll-loops")*/ #include <bits/stdc++.h> #define bit(n, i) (((n)>>(i))&1) #define cat(x) cout << #x << " = " << x << "\n" #define pb push_back #define mp make_pair #define se second #define fi first #define inf 2000000000 #define llinf 2000000000000000000LL #define forn(i, a, b) for(ll i = (a); i <= (b); ++i) #define all(x) (x).begin(), (x).end() #define ppc(x) __builtin_popcount(x) using namespace std; typedef long long ll; typedef double ld; typedef pair<int, int> i2; typedef vector<int> vi; typedef vector<ll> vll; const int N = 5e5+9; bool vis[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; int dcnt = 0; ll res = 0; bool ok = false; forn(i, 1, n) { int a; cin >> a; if(!vis[a]) { vis[a] = true; dcnt++; res += i-dcnt; if(dcnt == k) { ok = true; break; } } } cout << (ok ? res : -1); return 0; } |