#include <iostream> #include <string> using namespace std; int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); unsigned long long suma=0; int credit=0; int n,k,bufor,i=0; cin>>n>>k; bool ex[n]; for(int i=0;i<n;i++) { ex[i]=false; } while(k>0&&i<n) { cin>>bufor; if(ex[bufor-1]) { credit++; } else { ex[bufor-1]=true; suma+=credit; k--; } i++; } if(k==0) { cout<<suma; } else { cout<<-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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <iostream> #include <string> using namespace std; int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); unsigned long long suma=0; int credit=0; int n,k,bufor,i=0; cin>>n>>k; bool ex[n]; for(int i=0;i<n;i++) { ex[i]=false; } while(k>0&&i<n) { cin>>bufor; if(ex[bufor-1]) { credit++; } else { ex[bufor-1]=true; suma+=credit; k--; } i++; } if(k==0) { cout<<suma; } else { cout<<-1; } } |