1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

typedef long long ll;

int main() {
	int n, k;
	std::cin >> n >> k;
	bool* brands = new bool[n + 1]{ false };
	ll total = 0;
	int target = 0;
	for (int i = 0; i < n; ++i) {
		int brand;
		std::cin >> brand;
		if (target < k && !brands[brand]) {
			brands[brand] = true;
			total += (static_cast<ll>(i) - target++);
		}
	}
	
	std::cout << ((target < k) ? -1 : total) << std::endl;
	return 0;
}