#include <iostream> int64_t n, k, tmp, result, currentPos; bool sodasSet[500001]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cin >> n >> k; for (int64_t i = 0; i < n; ++i) { std::cin >> tmp; if (currentPos < k && !sodasSet[tmp]) { sodasSet[tmp] = true; result += (i - currentPos); ++currentPos; } } std::cout << (currentPos == k ? result : -1); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <iostream> int64_t n, k, tmp, result, currentPos; bool sodasSet[500001]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cin >> n >> k; for (int64_t i = 0; i < n; ++i) { std::cin >> tmp; if (currentPos < k && !sodasSet[tmp]) { sodasSet[tmp] = true; result += (i - currentPos); ++currentPos; } } std::cout << (currentPos == k ? result : -1); return 0; } |