#include <iostream> const long MAX = 500000; bool O[MAX]; int main() { std::ios_base::sync_with_stdio(0); long n,k,l=0; std::cin >> n >> k; long r = 0; for (long i=0;i<n;++i) { long a; std::cin >> a; if (l >= k) continue; if (O[a]) continue; O[a] = true; r += i-l; ++l; } if (l != k) r = -1; std::cout << r; 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 | #include <iostream> const long MAX = 500000; bool O[MAX]; int main() { std::ios_base::sync_with_stdio(0); long n,k,l=0; std::cin >> n >> k; long r = 0; for (long i=0;i<n;++i) { long a; std::cin >> a; if (l >= k) continue; if (O[a]) continue; O[a] = true; r += i-l; ++l; } if (l != k) r = -1; std::cout << r; return 0; } |