Problem: Example

Input

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


Output

11

Solution (in CPP):

Solution:
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

vector<int> G[100010];
int dpn[200010], dpc[200010];
 
signed main() {
    int V, E; cin >> V >> E;

    vector<int> visited(V, -1);
    for(int i=0; i<E; i++) {
        int u, v; cin >> u >> v;
        u--; v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }

    bool bipartite = true;
    int cnt0 = 0, cnt1 = 0, sum = 0, add = 0;
    function<void(int)> check = [&](int cur) {
        int c = visited[cur];
        for(auto to : G[cur]) {
            if(visited[to] >= 0) {
                bipartite &= (visited[to] == 1 - c);
            }
            else {
                visited[to] = 1 - c;
                (1 - c == 0 ? cnt0 : cnt1)++;
                check(to);
            }
        }
    };

    map<int, int> rec;
    for(int i=0; i<V; i++) {
        cnt0 = 0, cnt1 = 0;
        if(visited[i] >= 0) continue;
        visited[i] = 0; cnt0++;
        
        check(i);
        int val = abs(cnt0 - cnt1);
        sum += val * 2;
        add += min(cnt0, cnt1);
        rec[val]++;
    }

    if(!bipartite) {
        cout << -1 << endl;
        return 0;
    }

    fill(dpc, dpc + sum + 1, -1);
    fill(dpn, dpn + sum + 1, -1);
    dpc[0] = 0;
    for(auto e : rec) {
        int val, cnt; tie(val, cnt) = e;
        for(int i=0; i<=sum; i++) {
            if(dpc[i] >= 0) chmax(dpn[i], cnt);
            if(i + 2*val <= sum) chmax(dpn[i + 2*val], dpn[i] - 1);
            dpc[i] = -1;
        }
        swap(dpn, dpc);
    }

    int ans = 0;
    for(int i=0; i<=sum; i++) {
        if(dpc[i] < 0) continue;
        int A = i / 2 + add, B = V - A;
        chmax(ans, A * B - E);
    }
    cout << ans << endl;
    return 0;
}