Problem: Example

Input

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


Output

11

Solution (in CPP):

Solution:
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
//#define mt make_tuple
//typedef tuple<double,int,double> tp;
//typedef vector<tp> vt;
const double PI=acos(-1);
const double EPS=1e-7;
const int inf=1e9;
const ll INF=2e18;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
#define int ll
int n,m;
vvi G;
vi used,in,tmp;
int dfs(int a,int b=0){
	tmp.pb(a);
	used[a]=b;
	rep(i,G[a].size()){
		int to=G[a][i];
		if(used[to]==2){
			if(!dfs(to,b^1))return 0;
		}else if(used[to]==b)return 0;
	}
	return 1;
}
signed main(){
	cin>>n>>m;
	G=vvi(n);
	rep(i,m){
		int a,b;
		cin>>a>>b;
		a--;b--;
		G[a].pb(b);
		G[b].pb(a);
	}
	used=vi(n,2);
	in=vi(n);
	ll sum=0;
	rep(i,n)if(used[i]==2){
		tmp=vi(0);
		if(!dfs(i)){
			cout<<-1<<endl;
			return 0;
		}
		ll co=0;
		rep(j,tmp.size())if(used[tmp[j]]==1)co++;else co--;
		in[abs(co)]++;
		sum+=abs(co);
	}
	vi dp(100010,-1);
	dp[0]=0;
	rep(i,n)if(in[i])rep(j,100010){
		if(dp[j]>=0)dp[j]=in[i];
		else if(j<i||dp[j-i]<=0)dp[j]=-1;
		else dp[j]=dp[j-i]-1;
	}
	ll t=INF;
	rep(i,100010)if(dp[i]>=0)t=min(t,abs(i-(sum-i)));
	cout<<(n*n-t*t)/4-m<<endl;
}