#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 500007;
int tab[N];
bool bylo[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
ll n, k, x=1, wynik=0;
cin>>n>>k;
for(int i=1; i<=n; i++) cin>>tab[i];
for(int i=1; i<=k; i++) {
while(bylo[tab[x]]) x++;
if(x>n) {
cout<<"-1";
return 0;
}
wynik+=x-i;
bylo[tab[x]]=1;
}
cout<<wynik;
}
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 | #include<bits/stdc++.h> using namespace std; typedef long long int ll; const int N = 500007; int tab[N]; bool bylo[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); ll n, k, x=1, wynik=0; cin>>n>>k; for(int i=1; i<=n; i++) cin>>tab[i]; for(int i=1; i<=k; i++) { while(bylo[tab[x]]) x++; if(x>n) { cout<<"-1"; return 0; } wynik+=x-i; bylo[tab[x]]=1; } cout<<wynik; } |
English