Problem: Example

Input

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


Output

11

Solution (in CPP):

Solution:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) container.begin(), container.end()
#define RALL(container) container.rbegin(), container.rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 1000000007;

typedef vector< vector<int> > Graph;
pii dfs(Graph &g, vector<int> &color, int v, int c){
	color[v] = c;
	pii res(c==1, c==-1);
	FOR(it, g[v]){
		if(color[*it] == c) return pii(-1, -1);
		pii ret(0, 0);
		if(!color[*it] && (ret = dfs(g, color, *it, -c)).first < 0) return pii(-1, -1);
		res.first += ret.first;
		res.second += ret.second;
	}
	return res;
}

vi isBipartite(Graph &g){
	int n = g.size();
	vector<int> color(n);
	vector<int> knap;
	REP(i, n){
		pii ret(0, 0);
		if(!color[i] && (ret = dfs(g, color, i, 1)).first < 0) return vi();
		if(ret.first || ret.second){
			if(ret.first < ret.second) swap(ret.first, ret.second);
			knap.push_back(ret.first - ret.second);
		}
	}
	return knap;
}

ll n, m;

int main(){
	ios::sync_with_stdio(false);
	cin >> n >> m;
	Graph g(n);
	REP(i, m){
		int u, v;
		cin >> u >> v;u--;v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	int ans;
	vi knap = isBipartite(g);
	if(knap.empty()) cout << -1 << endl;
	else{
		vi dp(n+1);
		dp[0] = 1;
		int sum = 0;
		REP(i, knap.size()){
			RREP(j, sum+1) dp[j+knap[i]] |= dp[j];
			sum += knap[i];
		}
		ll ans = 0;
		int geta = (n - sum)/2;
		REP(i, sum+1) if(dp[i]) ans = max(ans, (ll)(i+geta)*(sum-i+geta));
		cout << ans - m << endl;
	}
	return 0;
}