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
#include <iostream>
#include <string_view>
#include <unordered_set>
#include <vector>

struct ora_t
{
        uint32_t shelf;
        uint32_t num_left;
        std::unordered_set<uint32_t> brands;
        std::vector<uint32_t> bottles;

        std::vector<uint32_t> dups;
        std::vector<uint32_t> nodups;

        ora_t()
                : shelf(0)
                , num_left(0)
                , brands()
                , bottles()
        {
        }


        ~ora_t()
        {
        }

        friend std::ostream& operator<<(std::ostream& out, ora_t const & o)
        {
                out << "s:" << o.shelf << " l:" << o.num_left;
                out << " b:" << o.brands.size() << "\n[";
                for (auto const & e : o.bottles)
                {
                        out << e << ", ";
                }
                out << "]\n[";
                for (auto const & e : o.dups)
                {
                        out << e << ", ";
                }
                out << "]\n[";
                for (auto const & e : o.nodups)
                {
                        out << e << ", ";
                }
                out << "]\n";
                return out;
        }

        friend std::istream& operator>>(std::istream& in, ora_t& o)
        {
                in >> o.shelf;
                in >> o.num_left;
                o.bottles.clear();
                o.bottles.reserve(o.shelf);
                o.brands.clear();
                for (std::size_t i = 0; i < o.shelf; ++i)
                {
                        uint32_t bottle = 0;
                        in >> bottle;
                        if (o.brands.find(bottle) != o.brands.end())
                        {
                                 if (i < o.num_left)
                                 {
                                        o.dups.emplace_back(i);
                                 }
                        }
                        else
                        {
                                if (i >= o.num_left)
                                {
                                        o.nodups.emplace_back(i);
                                }
                        }
                        o.brands.insert(bottle);
                        o.bottles.emplace_back(bottle);;
                }
                return in;
        }


};

ora_t read_input(std::istream& input, std::string& r_str)
{
        ora_t ora;
        input >> ora;
        return ora;
}

uint32_t solution(std::istream& input, std::ostream& out)
{
        std::string str;
        auto ora = read_input(input, str);
        int32_t result = 0;
        //out << ora;

        if (ora.dups.size() > ora.nodups.size())
        {
                result = -1;
        }
        else
        {
                for(auto dup =  ora.dups.begin(), nodup = ora.nodups.begin(); dup != ora.dups.end(); ++dup, ++nodup)
                {
                        result += *nodup - *dup;
                }
        }


        out << result << std::endl;
        return result;
}

#ifndef TEST
int main(int argc, char* argv[])
{
        solution(std::cin, std::cout);
        return 0;
}
#endif