#include <iostream>
#include <algorithm>
using namespace std;
int vis[100005];
int main()
{
int a,b,temam=0,liczba=0,tychniemam=0;
cin>>a>>b;
int tab[a];
for(int i=0;i<a;i++)
cin>>tab[i];
for(int i=0;i<b;i++)
{
if(vis[tab[i]]>0)
{
temam+=i;
liczba++;
}
vis[tab[i]]++;
}
for(int i=b;i<a;i++)
{
if(vis[tab[i]]==0&&liczba>0)
{
vis[tab[i]]=1;
tychniemam+=i;
liczba--;
}
}
//cout<<temam<<" "<<tychniemam<<" ";
if(liczba!=0)
{
cout<<-1<<endl; return 0;
}
cout<<tychniemam-temam<<endl;
}
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 | #include <iostream> #include <algorithm> using namespace std; int vis[100005]; int main() { int a,b,temam=0,liczba=0,tychniemam=0; cin>>a>>b; int tab[a]; for(int i=0;i<a;i++) cin>>tab[i]; for(int i=0;i<b;i++) { if(vis[tab[i]]>0) { temam+=i; liczba++; } vis[tab[i]]++; } for(int i=b;i<a;i++) { if(vis[tab[i]]==0&&liczba>0) { vis[tab[i]]=1; tychniemam+=i; liczba--; } } //cout<<temam<<" "<<tychniemam<<" "; if(liczba!=0) { cout<<-1<<endl; return 0; } cout<<tychniemam-temam<<endl; } |
English