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
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

template<typename T>
std::vector<T> read_as_vector(std::istream& stream, const int element_count);

void solve_dataset();
int number_of_shirts_to_giveaway(
    const std::vector<int>& contestants,
    const int min_tshirt_count
);

int main() {
    std::ios_base::sync_with_stdio(false);
    solve_dataset();
    return 0;
}

void solve_dataset() {
    int contestant_count, min_tshirt_count;
    std::cin >> contestant_count >> min_tshirt_count;
    auto contestants = read_as_vector<int>(std::cin, contestant_count);
    std::sort(contestants.begin(), contestants.end(), std::greater<int>());
    const int answer = number_of_shirts_to_giveaway(
        contestants,
        min_tshirt_count
    );
    std::cout << answer << std::endl;
}

template<typename T>
std::vector<T> read_as_vector(std::istream& stream, const int element_count) {
    std::vector<T> result(element_count);
    for(auto& element : result)
        stream >> element;
    return result;
}

int number_of_shirts_to_giveaway(
    const std::vector<int>& contestants,
    const int min_tshirt_count
) {
    const int size = static_cast<int>(contestants.size());
    int count = min_tshirt_count;
    while(count < size && contestants[count] == contestants[count - 1])
        count++;
    return count;
}