Problem: Example

Input

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


Output

11

Solution (in CPP):

Solution:
#include<algorithm>
#include<cassert>
#include<climits>
#include<cmath>
#include<cstring>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;

#define int int64_t
#define REP(i, x, n) for(int i = (x), i##_len = int(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = int(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = int(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = int(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = int(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << endl

constexpr int IINF = (1 << 30) - 1;
constexpr long long LLINF = 1LL << 61;
constexpr double EPS = 1e-8;

const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};


class UnionFind{
    vector<int> par;
    vector<long long> wei;
    int groupCount;

public:
    UnionFind(int n = 0){
        init(n);
    }

    void init(int n = 0){
        par.resize(n);
        fill(par.begin(), par.end(), -1);

        wei.resize(n);
        fill(wei.begin(), wei.end(), 0LL);

        groupCount = n;

        return;
    }

    int root(int x){
        if(par[x] < 0){
            return x;
        }
        int r = root(par[x]);
        wei[x] += wei[par[x]];
        return par[x] = r;
    }

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

    int size(int x){
        return -par[root(x)];
    }

    long long weight(int x){
        root(x);
        return wei[x];
    }

    long long diff(int x, int y){
        return weight(y) - weight(x);
    }

    bool unite(int x, int y, long long w = 0){
        w += weight(x) - weight(y);

        x = root(x);
        y = root(y);

        if(x == y){
            return false;
        }

        if(par[y] < par[x]){
            swap(x, y);
            w = -w;
        }

        par[x] += par[y];
        par[y] = x;
        wei[y] = w;
        --groupCount;

        return true;
    }

    int size(void){
        return groupCount;
    }
};

template<typename T>
bool chmax(T& a, T b, bool equal = false){
    if(a < b || equal && a == b){
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool chmin(T& a, T b, bool equal = false){
    if(b < a || equal && a == b){
        a = b;
        return true;
    }
    return false;
}

void dfs(int v, int d, const vector< vector<int> >& g, vector<int>& c){
    c[v] = d;

    for(auto to : g[v]){
        if(c[to] < 0){
            dfs(to, d ^ 1, g, c);
        }
    }

    return;
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int v, e;
    cin >> v >> e;

    vector< vector<int> > g(v);
    UnionFind uf(v);
    rep(i, e){
        int a, b;
        cin >> a >> b;
        g[--a].emplace_back(--b);
        g[b].emplace_back(a);
        uf.unite(a, b);
    }

    vector<int> c(v, -1);
    rep(i, v){
        if(c[i] < 0){
            dfs(i, 0, g, c);
        }
    }

    rep(i, v){
        for(auto to : g[i]){
            if(c[i] == c[to]){
                cout << -1 << endl;
                return 0;
            }
        }
    }

    vector< vector<int> > cnt(2, vector<int>(v, 0));
    int t = 0;
    rep(i, v){
        if(uf.size(i) == 1){
            ++t;
        }else{
            ++cnt[c[i]][uf.root(i)];
        }
    }

    unordered_map<int, int> mp;
    int mi = 0;
    int ma = 0;
    int lim = 0;
    rep(i, v){
        if(0 < cnt[0][i]){
            ++mp[abs(cnt[0][i] - cnt[1][i])];
            mi += min(cnt[0][i], cnt[1][i]);
            ma += max(cnt[0][i], cnt[1][i]);
            lim += abs(cnt[0][i] - cnt[1][i]);
        }
    }

    vector<int> a, m;
    for(auto it = mp.begin() ; it != mp.end() ; ++it){
        a.emplace_back((*it).first);
        m.emplace_back((*it).second);
    }
    m.emplace_back(0);

    vector< vector<int> > dp(2, vector<int>(lim + 1, -1));
    dp[0][0] = m[0];

    rep(i, a.size()){
        rep(j, lim + 1){
            if(dp[i & 1][j] >= 0){
                dp[(i + 1) & 1][j] = m[i + 1];
            }
            if(j + a[i] <= lim){
                chmax(dp[i & 1][j + a[i]], dp[i & 1][j] - 1);
            }
        }
        rep(j, lim + 1){
            dp[i & 1][j] = -1;
        }
    }

    int k = 0;
    int mini = ma - mi;
    rep(j, lim + 1){
        if(dp[((signed)a.size()) & 1][j] >= 0 && chmin(mini, abs(ma - j - mi - j))){
            k = j;
        }
    }

    mi += k;
    ma -= k;

    while(t){
        if(mi < ma){
            ++mi;
        }else{
            ++ma;
        }
        --t;
    }

    cout << (lint)(mi * ma) - e << endl;

    return 0;
}