Problem: Example

Input

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


Output

11

Solution (in CPP):

Solution:
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

typedef pair<int, int> Pi;
typedef tuple<int, int, int> Ti;
typedef vector<int> vint;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

int V, E;
vector< vector<int> > graph;
int color[100010];

Pi dfs(int u, int c) {
  Pi res(0, 0);
  color[u] = c;
  if(c == 1) res.first++;
  else res.second++;
  for(int v : graph[u]) {
    if(color[v] == c) return Pi(-1, -1);
    if(color[v] == 0) {
      Pi tmp = dfs(v, -c);
      if(tmp.first < 0) return Pi(-1, -1);
      res.first += tmp.first;
      res.second += tmp.second;
    }
  }
  return res;
}

map<int, int> mp;

int dp[100010]; // テ・ツ?凝ヲツ閉ーテ・ツ按カテゥツ卍静、ツサツ佚」ツ?催ゥツδィテ・ツ按?・ツ陳?テ」ツ??」ツ?湘」ツ?、テ」ツ?凝」ツ?ョテ・ツョツ古・ツ?ィテ、ツコツ古ゥツδィテ」ツつーテ」ツδゥテ」ツδ陛」ツつ津」ツ?湘」ツ?」テ」ツ?、テ」ツ?妥」ツ?ヲテ」ツつづ・ツョツ古・ツ?ィテ、ツコツ古ゥツδィテ」ツつーテ」ツδゥテ」ツδ陛」ツ?ォテ」ツ?ァテ」ツ?催」ツ?敕」ツ??」ツ?ェテ」ツ?ョテ」ツ?ァテ」ツ??」ツ??ヲツ?淌」ツ?佚」ツ?ォテ」ツ?湘」ツ?」テ」ツ?、テ」ツ?妥」ツ?淌」ツ??」ツ?づゥツサツ津」ツつ津」ツ?ゥテ」ツつ古」ツ??」ツ?妥・ツ「ツ療」ツつ?」ツ?崚」ツつ凝」ツ??

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> V >> E;
  resz(graph, V);
  rep(i, E) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  mp.clear();
  memset(color, 0, sizeof(color));
  int v = 0; // テ、ツコツ古ゥツδィテ」ツつーテ」ツδゥテ」ツδ陛」ツ?ョテァツ可?ヲツ鳴ケテ」ツ?ァテ、ツスツ愿」ツつ古」ツつ凝ヲツ慊?・ツーツ湘」ツ?ョテゥツδィテ・ツ按?ゥツ崢?・ツ青暗」ツ?ョテ・ツ、ツァテ」ツ?催」ツ??
  rep(i, V) {
    if(color[i] != 0) continue;
    Pi p = dfs(i, 1);
    int a = min(p.first, p.second);
    int b = max(p.first, p.second);
    if(a < 0) {
      cout << -1 << endl;
      return 0;
    }
    mp[b-a]++;
    v += a;
  }

  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  for(auto p : mp) {
    int plus = p.first, num = p.second;
    rep(i, V+1) {
      if(dp[i] >= 0) dp[i] = num;
      else if(i < plus || dp[i-plus] <= 0) dp[i] = -1;
      else dp[i] = dp[i-plus]-1;
    }
  }
  int ans = 0;
  rep(i, V+1) if(dp[i] >= 0) chmax(ans, (v+i)*(V-v-i));
  cout << ans-E << endl;

  return 0;
}