#include <iostream>
#include <algorithm>
using namespace std;
int tab[10000], wyn[10000], x=0;
void Search(int n, int p)
{
int l=1, c=1;
while(tab[l]!=n)
{
l++;
if(l>p)
{
c=0;
x++;
break;
}
}
if(c==0) wyn[n]=500005;
else wyn[n]=l;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int a, b, sum=0;
cin>>a>>b;
for(int i=1; i<=a; i++)
{
cin>>tab[i];
}
for(int j=1; j<=a; j++)
{
Search(j, a);
}
if((a-x)<b) cout<<"-1";
else
{
sort(wyn, wyn+(a+1));
for(int k=1; k<=b; k++)
{
sum=(wyn[k]-k)+sum;
}
cout<<sum;
}
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 44 45 46 | #include <iostream> #include <algorithm> using namespace std; int tab[10000], wyn[10000], x=0; void Search(int n, int p) { int l=1, c=1; while(tab[l]!=n) { l++; if(l>p) { c=0; x++; break; } } if(c==0) wyn[n]=500005; else wyn[n]=l; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int a, b, sum=0; cin>>a>>b; for(int i=1; i<=a; i++) { cin>>tab[i]; } for(int j=1; j<=a; j++) { Search(j, a); } if((a-x)<b) cout<<"-1"; else { sort(wyn, wyn+(a+1)); for(int k=1; k<=b; k++) { sum=(wyn[k]-k)+sum; } cout<<sum; } return 0; } |
English