#include<bits/stdc++.h>
using namespace std;
int n,k,tab[500007];
set <int> s;
long long wynik;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
if(k==1) {cout << 0;return 0;}
for(int i=1;i<=n;i++)
{
cin >> tab[i];
}
s.insert(tab[1]);
int l=2;
for(int i=2;i<=n && l<=k;i++)
{
if(s.find(tab[i])!=s.end()) continue;
s.insert(tab[i]);
wynik-=l;
wynik+=i;
l++;
}
if(l<=k) {cout << "-1";return 0;}
cout << wynik;
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 | #include<bits/stdc++.h> using namespace std; int n,k,tab[500007]; set <int> s; long long wynik; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; if(k==1) {cout << 0;return 0;} for(int i=1;i<=n;i++) { cin >> tab[i]; } s.insert(tab[1]); int l=2; for(int i=2;i<=n && l<=k;i++) { if(s.find(tab[i])!=s.end()) continue; s.insert(tab[i]); wynik-=l; wynik+=i; l++; } if(l<=k) {cout << "-1";return 0;} cout << wynik; return(0); } |
English