Problem: Example

Input

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


Output

11

Solution (in CPP):

Solution:
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int,int> pint;
typedef pair<ll,ll> pll;

#define FI first
#define SE second
#define PB push_back
#define MP make_pair
#define ALL(s) (s).begin(),(s).end()
#define REP(i,n) for (int i = 0; i < (int)(n); ++i)
#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl

template<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<", "<<P.second<<'>';}
template<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<"{ ";for(int i=0;i<P.size();++i){if(i>0)s<<", ";s<<P[i];}return s<<" }"<<endl;}
template<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<"{ ";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<", ";s<<'<'<<it->first<<"->"<<it->second<<'>';}return s<<" }"<<endl;}


const int INF = 1<<29;
const int MAX_V = 100100;
struct Graph {
    int V;
    vector<int> list[MAX_V];
    
    Graph(int n = 0) : V(n) {}
    void init(int n = 0) {V = n; for (int i = 0; i < MAX_V; ++i) list[i].clear();}
    inline vector<int>& operator [] (int i) {return list[i];}
    
    void connect(int f, int t) {
        list[f].push_back(t);
        list[t].push_back(f);
    }
} G; 


map<ll, int> ma;
vector<pll> item;

int color[MAX_V];
bool bidfs(Graph &G, int v, int c, vector<int> &left, vector<int> &right) {
    color[v] = c;
    if (c == 1) left.push_back(v);
    else right.push_back(v);
    for (int i = 0; i < G[v].size(); ++i) {
        if (color[G[v][i]] == c) return false;
        if (color[G[v][i]] == 0) {
            if (!bidfs(G, G[v][i], -c, left, right)) return false;
        }
    }
    return true;
}

bool Isbipartite(Graph &G, vector<int> &left, vector<int> &right) {
    memset(color, 0, sizeof(color));
    left.clear(); right.clear();
    for (int i = 0; i < G.V; ++i) {
        if (color[i] == 0) {
            if (!bidfs(G, i, 1, left, right)) return false;
            else {
                int num = abs((int)left.size() - (int)right.size());
                ma[num]++;
                left.clear(); right.clear();
            }
        }
    }
    return true;
}


ll V, E, from, to;
ll dp[MAX_V*2], tdp[MAX_V*2];

ll solve() {
    vint left, right;
    if (!Isbipartite(G, left, right)) return -1;
    EACH(it,ma) if (it->FI != 0) item.PB(MP(it->FI, it->SE));
    
    //COUT(item);
    
    int n = item.size();
    for (int i = 0; i < MAX_V*2; ++i) dp[i] = tdp[i] = INF;
    dp[MAX_V] = 0;
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j < MAX_V*2; ++j) {
            //if (dp[j] < INF) cout << i << ", " << j-MAX_V << " : " << dp[j] << endl;
        }
        if (i == n) break;
            
        ll sub = item[i].FI * item[i].SE;
        for (int j = 0; j < MAX_V*2; ++j) {
            if (j >= sub && dp[j] < INF) tdp[j-sub] = 0;
        }
        for (int j = 0; j < MAX_V*2; ++j) {
            if (j >= item[i].FI*2) {
                if (tdp[j - item[i].FI*2] < item[i].SE) 
                    tdp[j] = min(tdp[j], tdp[j - item[i].FI*2] + 1);
            }
        }
        for (int j = 0; j < MAX_V*2; ++j) {
            dp[j] = tdp[j], tdp[j] = INF;
        }
    }
    
    ll res = INF;
    for (int i = 0; i < MAX_V*2; ++i) {
        if (dp[i] < INF) {
            res = min(res, (ll)abs(i-MAX_V));
        }
    }
    //COUT(res);
    
    ll a = (V + res) / 2, b = (V - res) / 2;

    return a * b - E;
}

int main() {
    while (cin >> V >> E) {
        ma.clear(); item.clear();
        G.init(V);
        for (int i = 0; i < E; ++i) {
            scanf("%d %d", &from, &to);
            --from; --to;
            G.connect(from, to);
        }
        cout << solve() << endl;
    }
    
    return 0;
}