#include <iostream> using namespace std; int n, k; int but[500001]; bool ile[500001]; int got; long long int wynik; int i = 0; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; for(int i = 0; i < n; ++i){ cin >> but[i]; } while(got < k && i < n){ if(!ile[but[i]]){ wynik += i - got; ++got; ile[but[i]] = true; } ++i; } if(got < k) cout << -1; else cout << wynik; }
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 | #include <iostream> using namespace std; int n, k; int but[500001]; bool ile[500001]; int got; long long int wynik; int i = 0; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; for(int i = 0; i < n; ++i){ cin >> but[i]; } while(got < k && i < n){ if(!ile[but[i]]){ wynik += i - got; ++got; ile[but[i]] = true; } ++i; } if(got < k) cout << -1; else cout << wynik; } |