#include <bits/stdc++.h> using namespace std; long long int n,k,xd; long long int t[500001]; bool bul[500001]; long long int d,w; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; for(int i=0;i<n;i++) cin >> t[i]; for(int i=0;i<k;i++) { if(!bul[t[i]]) d++; else xd+=i+1; bul[t[i]]=1; } d=k-d; //cout << d << endl; int it=k; while(d) { if(it==n) { cout << -1; return 0; } if(!bul[t[it]]) { bul[t[it]]=1; w+=it+1; //cout << it << endl; d--; } it++; } cout << w-xd; 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 | #include <bits/stdc++.h> using namespace std; long long int n,k,xd; long long int t[500001]; bool bul[500001]; long long int d,w; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; for(int i=0;i<n;i++) cin >> t[i]; for(int i=0;i<k;i++) { if(!bul[t[i]]) d++; else xd+=i+1; bul[t[i]]=1; } d=k-d; //cout << d << endl; int it=k; while(d) { if(it==n) { cout << -1; return 0; } if(!bul[t[it]]) { bul[t[it]]=1; w+=it+1; //cout << it << endl; d--; } it++; } cout << w-xd; return 0; } |