Problem: Example

Input

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


Output

11

Solution (in CPP):

Solution:
#include <iostream>
#include <fstream>
#include <cassert>
#include <typeinfo>
#include <vector>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#include <random>
#include <complex>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<29;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-7;
const ll mod=1e9+7;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

int n,m;
vvi g;

P dfs(int v,int b,vi& used){
	used[v]=b;
	P p;
	if(b) p.first++;
	else p.second++;
	for(int i=0;i<g[v].size();i++){
		int u=g[v][i];
		if(used[u]==-1){
			P q=dfs(u,!b,used);
			p.first+=q.first;
			p.second+=q.second;
		}
		if(used[u]==b) return {-inf,0};
	}
	return p;
}

int main(){
	cin>>n>>m;
	g=vvi(n);
	for(int i=0;i<m;i++){
		int u,v;
		cin>>u>>v;
		u--;v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vi used(n,-1);
	int tmp=0;
	map<int,int> M;
	for(int i=0;i<n;i++) if(used[i]==-1){
		P p=dfs(i,1,used);
		int A=p.first,B=p.second;
		if(A<0){
			cout<<-1<<endl;
			return 0;
		}
		tmp+=min(A,B);
		M[abs(A-B)]++;
	}
	vi dp(n+1);
	dp[tmp]=1;
	for(auto i=M.begin();i!=M.end();i++){
		int v=i->first,N=i->second;
		vi DP(n+1,-1);
		for(int j=0;j<=n;j++){
			if(dp[j]) DP[j]=0;
			else if(j>=v&&DP[j-v]!=-1&&DP[j-v]<N){
				DP[j]=DP[j-v]+1;
				dp[j]|=1;
			}
		}
	}
	ll res=0;
	for(int i=0;i<=n;i++) res=max(res,(ll)dp[i]*i*(n-i));
	cout<<res-m<<endl;
}