Problem: Example

Input

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


Output

11

Solution (in CPP):

Solution:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

bool ok = true;

int f(int u, int x, vector<vector<int> >& G, vector<int>& vis) {
	vis[u] = x;
	int sum = 1;
	for (int i = 0; i < G[u].size(); i++) {
		int v = G[u][i];
		if (vis[v] == x) ok = false;
		if (vis[v] != -1) continue;
		sum -= f(v, !x, G, vis);
	}
	return sum;
}

int main() {
	int N, M; cin >> N >> M;
	vector<vector<int> > G(N);
	for (int i = 0; i < M; i++) {
		int x, y; cin >> x >> y;
		x--; y--;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	vector<int> vis(N, -1);
	int n = 0;
	vector<int> a(N + 1);
	for (int u = 0; u < N; u++)
		if (vis[u] == -1) {
			int x = abs(f(u, 0, G, vis));
			n += x;
			a[x]++;
		}
	if (!ok) {
		cout << -1 << endl;
		return 0;
	}
	vector<bool> dp(n + 1);
	dp[0] = true;
	for (int x = 1; x <= N; x++)
		for (int y = 1; a[x]; y *= 2) {
			int z = min(a[x], y);
			a[x] -= z;
			for (int k = n; k - z * x >= 0; k--)
				if (dp[k - z * x])
					dp[k] = true;
		}
	ll ma = 0;
	for (int k = 0; k <= n; k++)
		if (dp[k])
			ma = max(ma, (ll)((N - n) / 2 + k) * ((N - n) / 2 + (n - k)));
	cout << ma - M << endl;
}