#include<bits/stdc++.h> using namespace std; set<int> s; int main() { int n,k, bottle; long long result=0; cin>>n>>k; long long originalK = k; for(int i=1;i<=n;i++) { cin>>bottle; if(s.find(bottle)==s.end()) { s.insert(bottle); k--; result+=i; } if(k==0) { cout<<result - (originalK*(originalK+1)/2)<<endl; return 0; } } cout<<-1<<endl; 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 | #include<bits/stdc++.h> using namespace std; set<int> s; int main() { int n,k, bottle; long long result=0; cin>>n>>k; long long originalK = k; for(int i=1;i<=n;i++) { cin>>bottle; if(s.find(bottle)==s.end()) { s.insert(bottle); k--; result+=i; } if(k==0) { cout<<result - (originalK*(originalK+1)/2)<<endl; return 0; } } cout<<-1<<endl; return 0; } |