Feirinha de Artesanato

Problem Link

Solution by mateusgrl39100 pts

Code / Notes

int N;
    cin >> N;

    vector<int> t(N), p(N);

    for (int i = 0; i < N; i++) cin >> t[i];
    for (int i = 0; i < N; i++) cin >> p[i];

    vector<int> t1, t2;

    for (int i = 0; i < N; i++) {
        if (t[i] == 1) t1.push_back(p[i]);
        else t2.push_back(p[i]);
    }

    sort(t1.begin(), t1.end());
    sort(t2.begin(), t2.end());

    int i1 = 0, i2 = 0;
    long long total = 0;

    int C;
    cin >> C;

    for (int i = 0; i < C; i++) {
        int u;
        cin >> u;

        if (u == 1) {
            if (i1 < t1.size()) {
                total += t1[i1++];
            }
        } else if (u == 2) {
            if (i2 < t2.size()) {
                total += t2[i2++];
            }
        } else {
            // indeciso
            int v1 = (i1 < t1.size() ? t1[i1] : INT_MAX);
            int v2 = (i2 < t2.size() ? t2[i2] : INT_MAX);

            if (v1 == INT_MAX && v2 == INT_MAX) continue;

            if (v1 <= v2) {
                total += v1;
                i1++;
            } else {
                total += v2;
                i2++;
            }
        }
    }

    cout << total << endl;

Last updated 1 month, 3 weeks ago


« Back to problem