#include<bits/stdc++.h> using namespace std; typedef long long ll; #define SIZE(x) (int)x.size() #define REP(x,t) for(ll x=0; x<(t); x++) #define FOR(x,t) for(ll x=1; x<=(t); x++) #define pb(x) push_back(x) #define pf(x) push_front(x) #define st first #define nd second ll inf=1e9+7; ll mod=1e9+7; int tab[500005]; int main () { ios_base::sync_with_stdio(0); cin.tie(0); int n,k; ll w=0; cin>>n>>k; int j=0; int x; REP(i,n){ cin>>x; if(tab[x]==0){ if(k>0){ w+=i-j; j++; k--; } tab[x]++; } } if(k>0) cout<<"-1"; else cout<<w; 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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; #define SIZE(x) (int)x.size() #define REP(x,t) for(ll x=0; x<(t); x++) #define FOR(x,t) for(ll x=1; x<=(t); x++) #define pb(x) push_back(x) #define pf(x) push_front(x) #define st first #define nd second ll inf=1e9+7; ll mod=1e9+7; int tab[500005]; int main () { ios_base::sync_with_stdio(0); cin.tie(0); int n,k; ll w=0; cin>>n>>k; int j=0; int x; REP(i,n){ cin>>x; if(tab[x]==0){ if(k>0){ w+=i-j; j++; k--; } tab[x]++; } } if(k>0) cout<<"-1"; else cout<<w; return 0; } |