Problem: Example

Input

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


Output

11

Solution (in CPP):

Solution:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(__typeof((X).begin()) it=(X).begin();it!=(X).end();it++)
#define ite iterator
#define mp make_pair
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define sec second
#define sz(x) ((int)x.size())

using namespace std;

struct timer{
	time_t start;
	timer(){start=clock();}
	~timer(){cerr<<1.*(clock()-start)/CLOCKS_PER_SEC<<" secs"<<endl;}
};

typedef istringstream iss;
typedef long long ll;
typedef pair<int,int> pi;
typedef stringstream sst;
typedef vector<int> vi;

int V,E;
vi g[100010];
ll size;
map<int,int> M;
int dp[100010];

int color[100010];

pi dfs(int v,int c){
	pi res;
	if(c==1)res.fir=1;
	else res.sec=1;
	color[v]=c;
	rep(i,sz(g[v])){
		if(color[g[v][i]]==c)return mp(-1,-1);
		if(!color[g[v][i]]){
			pi res2=dfs(g[v][i],-c);
			if(res2.fir==-1)return mp(-1,-1);
			res.fir+=res2.fir;
			res.sec+=res2.sec;
		}
	}
	return res;
}

int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	
	cin>>V>>E;
	rep(i,E){
		int a,b;
		cin>>a>>b;
		a--;b--;
		g[a].pb(b);
		g[b].pb(a);
	}
	
	rep(i,V)if(!color[i]){
		pi res=dfs(i,1);
		if(res.fir==-1){
			cout<<"-1"<<endl;
			return 0;
		}
		if(res.fir>res.sec)swap(res.fir,res.sec);
		size+=res.fir;
		M[res.sec-res.fir]++;
	}
	int m=V/2-size;
	fill(dp,dp+m+1,-1);
	dp[0]=0;
	foreach(it,M){
		rep(i,m+1){
			if(dp[i]>=0){
				dp[i]=it->sec;
			}else if(i < it->fir || dp[i - it->fir] <= 0){
				dp[i]=-1;
			}else{
				dp[i]=dp[i - it->fir]-1;
			}
		}
	}
	for(int i=m;i>=0;i--)if(dp[i]>=0){
		size+=i;
		cout<<size*(V-size)-E<<endl;
		return 0;
	}
}