| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 1199941 | fadyscube | Catfish Farm (IOI22_fish) | C++17 | 0 ms | 0 KiB | 
#include "fish.h"
#include <vector>
#include <algorithm>
using namespace std;
#define ll long long
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
    ll can1 = 0;
    ll can2 = 0;
    for (int i = 0; i < M; i++) {
        if (X[i] == 0)
            can2 += W[i];
        else
            can1 += W[i];
    }
    if (N <= 2) {
        return max(can1, can2);
    } else {
        vector<pair<int, int>> v;
        for (int i = 0; i < M; i++) {
            v.push_back({Y[i], i});
        }
        struct {
            bool operator()(pair<int, int> a, pair<int, int> b) const {
                if (a.first == b.first) return X[a.second] < X[b.second];
                return a.first < b.first;
            }
        }
        customLess;
        sort(v.begin(), v.end(), customLess);
        ll tmp = can1;
        for (int i = 0; i < M;) {
            int j = v[i].second;
            if (i+1 < M && v[i].first == v[i+1].first) {
                tmp -= W[j];
                tmp += W[v[i+1].second];
                i += 2;
            } else if (X[j] == 0) {
                tmp += W[j];
                i++;
            } else {
                tmp -= W[j];
                i++;
            }
            can1 = max(tmp, can1);
        }
        return can1;
    }
}
