Problem: Example

Input

8 5
1 2
6 5
6 4
1 3
4 7


Output

11

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<ll> Size;
    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1);
        for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = i / (n / 2);
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        Size[x] += Size[y];
        return true;
    }

    ll size(int x){
        return Size[root(x)];
    }
};


int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N, M;
    cin >> N >> M;
    UnionFind uni(2 * N);
    for(int i = 0; i < M; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        uni.merge(a, b + N);
        uni.merge(b, a + N);
    }
    set<i_i> st;
    //ll maxi = 0;
    bitset<50001> bits;
    bits[0] = true;
    for(int i = 0; i < N; i++) {
        if(uni.issame(i, i + N)) {
            cout << -1 << endl;
            return 0;
        }
        i_i tmp = {uni.root(i), uni.root(i+N)};
        if(tmp.first < tmp.second) swap(tmp.first, tmp.second);
        if(st.count(tmp)) continue;
        st.insert(tmp);
        //cerr << i << " " << uni.size(i) << " " << uni.size(i+N) << endl;
        //maxi += max(uni.size(i), uni.size(i+N));
        bits = (bits << uni.size(i)) | (bits << uni.size(i+N));
    }
    /*
    cerr << maxi << endl;
    ll ans = maxi * (N - maxi) - M;
    cout << ans << endl;
    */
    for(ll i = N / 2; i >= 0; i--) {
        if(bits[i]) {
            cout << i * (N - i) - M << endl;
            return 0;
        }
    }
    return 0;
}