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
#include <bits/stdc++.h>

using namespace std;

struct TestCase
{
  size_t bottle_count, desired_count;
  vector<size_t> bottles;
};

TestCase read_test_case();
void solve_test_case(const TestCase&);

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve_test_case(read_test_case());
}

TestCase read_test_case()
{
  TestCase tc;
  cin >> tc.bottle_count >> tc.desired_count;
  tc.bottles.resize(tc.bottle_count);
  for (auto& bottle : tc.bottles) cin >> bottle;
  return tc;
}

void solve_test_case(const TestCase& tc)
{
  set<size_t> bottles;
  vector<size_t> distinct_positions;
  for (size_t i = 0; i < tc.bottle_count; i++)
  {
    if (bottles.count(tc.bottles[i]) != 0) continue;
    bottles.insert(tc.bottles[i]);
    distinct_positions.push_back(i);
  }

  if (bottles.size() < tc.desired_count)
  {
    cout << -1 << "\n";
    return;
  }

  long long moves = 0;
  for (size_t i = 0; i < tc.desired_count; i++)
    moves += static_cast<long long>(distinct_positions[i] - i);

  cout << moves << "\n";
}