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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#define TEST_SIZE 6

#include <iostream>
#include <vector>

using namespace std;

#ifdef DEBUG
#include <algorithm>
#include <climits>
#include <random>
#include <chrono>
#include <sstream>

mt19937_64 randomEngine(chrono::system_clock::now().time_since_epoch().count());

int random(int min, int max)
{
	return uniform_int_distribution<int>(min, max)(randomEngine);
}

void makeTest(stringstream& cout)
{
	cout << TEST_SIZE << ' ' << random(1, TEST_SIZE) << '\n';

	for(int index = 0; index < TEST_SIZE; index++)
	{
		cout << random(1, TEST_SIZE) << ' ';
	}

	cout << '\n';
}

void brute(stringstream& cin, stringstream& cout)
{
	int size, value;
	cin >> size >> value;

	vector<int> values(size), permutation;

	for(auto& value : values)
	{
		cin >> value;
	}

	for(int index = 0; index < size; index++)
	{
		permutation.push_back(index);
	}

	long long result = LLONG_MAX;

	do
	{
		vector<bool> used(size);
		bool correct = true;

		for(int index = 0; index < value; index++)
		{
			if(used[values[permutation[index]] - 1])
			{
				correct = false;
				break;
			}

			used[values[permutation[index]] - 1] = true;
		}

		if(!correct)
		{
			continue;
		}

		long long current = 0;

		for(int index = 0; index < size; index++)
		{
			current += max(permutation[index] - index, 0);
		}

		result = min(result, current);
	} while(next_permutation(permutation.begin(), permutation.end()));

	cout << (result == LLONG_MAX ? -1 : result) << '\n';
}

void solve(stringstream& cin, stringstream& cout)
#else
void solve()
#endif
{
	int size, value;
	cin >> size >> value;

	long long result = 0;
	int position = 0;

	vector<bool> used(size);

	for(int index = 0; index < size; index++)
	{
		int current;
		cin >> current;

		if(!used[current - 1])
		{
			used[current - 1] = true;
			result += index - position;
			position++;

			if(position == value)
			{
				cout << result << '\n';
				return;
			}
		}
	}

	cout << "-1\n";
}

int main()
{
#ifdef DEBUG
	for(int test = 1; test < INT_MAX; test++)
	{
		stringstream input, bruteOutput, solveOutput;
		makeTest(input);

		brute(input, bruteOutput);
		input.seekg(0);

		solve(input, solveOutput);
		input.seekg(0);

		if(bruteOutput.str() == solveOutput.str())
		{
			cout << "[#" << test << "] OK\n";
		}
		else
		{
			cout << "[#" << test << "] WRONG\nINPUT:\n" << input.str() << "BRUTE:\n" << bruteOutput.str() << "SOLVE:\n" << solveOutput.str();
			return 0;
		}
	}
#else
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	solve();
#endif
}