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
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>

using namespace std;

void printf(vector<int>arr) {
	cout << endl;
	for (int i = 0; i < arr.size(); i++)
		cout << " " << arr[i];
	cout << endl;
	return;
}

int find_soda(vector<int>arr,int n, int k) {
	vector<bool>exist(n + 1, false);

	int p = 1;
	int ans = 0;
	for (int i = 0; i < k; i++) {
		if (exist[arr[i]] == true) {
			while (exist[arr[p]] == true) {
				p++;
				if (p == n) return -1;
			}
			ans += p - i;
			exist[arr[p]] = true;
			swap(arr[i], arr[p]);

		}
		else 	exist[arr[i]] = true;

	}
	//printf(arr);
	return ans;
}

int solve() {
	int n, k;
	cin >> n >> k;
	vector<int> arr(n);
	vector<bool>exist(n+1, false);
	for (int i = 0; i < n; i++)cin >> arr[i];

	return find_soda(arr, n, k);
}

string zmien_string(int x) {
	string ans = "";
	while (x) {
		ans += x % 10 + '0';
		x /= 10;
	}
	return ans;
}

void check_solution(int num) {
	string input = "in\\";
	input += zmien_string(num);
	input += ".in";

	string output = "out\\";
	output += zmien_string(num);
	output += ".out";

	fstream wyjscie;
	wyjscie.open(output);
	int correct_ans;
	wyjscie >> correct_ans;
	wyjscie.close();

	fstream dane;
	dane.open(input);

	int n, k;
	dane >> n >> k;
	if (n < 0)return;
	vector<int> arr(n);
	vector<bool>exist(n + 1, false);
	for (int i = 0; i < n; i++) dane>> arr[i];
	dane.close();
	int x = find_soda(arr, n, k);
	cout << num <<": "<< endl;
	cout << correct_ans << "    " << x << endl;

	if (correct_ans == x)	cout << "TRUE" << endl;
	else cout << "FALSE" << endl;

}

void test(int n) {
	for (int i = 0; i < n; i++) {
		cout << endl;
		check_solution(i + 1);
		cout << endl;
	}
	return;
}

int main() {
	//test(50);
	cout<<solve();

	return 0;
}