#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int main()
{
BOOST;
ll n, k;
cin>>n>>k;
vector<bool> V(n);
ll wynik = 0;
ll pozprzen = 0;
for(ll i = 0; i<n && k>0; i++)
{
ll a;
cin>>a;
a--;
if(!V[a])
{
V[a] = true;
k--;
wynik+= i-pozprzen;
pozprzen++;
}
}
cout<< (k==0 ? wynik:-1);
}
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); int main() { BOOST; ll n, k; cin>>n>>k; vector<bool> V(n); ll wynik = 0; ll pozprzen = 0; for(ll i = 0; i<n && k>0; i++) { ll a; cin>>a; a--; if(!V[a]) { V[a] = true; k--; wynik+= i-pozprzen; pozprzen++; } } cout<< (k==0 ? wynik:-1); } |
English