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
#include <iostream>
#include <set>

int main()
{
    unsigned int participants{0};
    unsigned int shirts{0};

    std::cin >> participants >> shirts;

    std::multiset<unsigned int> ranks;
    unsigned int rank{0};
    for (unsigned int i = 0; i < participants; i++)
    {
        std::cin >> rank;
        ranks.insert(rank);
    }

    unsigned int real_shirts{0};
    unsigned int last_val{0};
    unsigned int round{0};
    for (auto ms_it = ranks.rbegin(); ms_it != ranks.rend() && round < shirts; ms_it++)
    {
        if (*ms_it != last_val)
        {
            ++round;
            last_val = *ms_it;
            if (round == shirts && *(ms_it++) == last_val)
                ++real_shirts;
        }
        ++real_shirts;
    }

    std::cout << real_shirts << std::endl;

    return 0;
}