Problem p00958 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
short dp[16][16][10][1 << 16];
template <typename T1, typename T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  Int n;
  cin >> n;
  using P = pair<Int, Int>;
  vector<P> v;
  for (Int i = 0; i < n; i++) {
    Int x, y;
    cin >> x >> y;
    v.emplace_back(x, y);
  }
  vector<vector<P>> r(n, vector<P>(n));
  for (Int i = 0; i < n; i++) {
    for (Int j = i + 1; j < n; j++) {
      if (v[i].first == v[j].first) {
        r[i][j] = P(0, 1);
        continue;
      }
      if (v[i].second == v[j].second) {
        r[i][j] = P(1, 0);
        continue;
      }
      Int dx = v[i].first - v[j].first;
      Int dy = v[i].second - v[j].second;
      if (dx < 0) {
        dx *= -1;
        dy *= -1;
      }
      Int k = __gcd(dx, abs(dy));
      dx /= k;
      dy /= k;
      r[i][j] = P(dx, dy);
      // cout<<i<<" "<<j<<":"<<dx<<" "<<dy<<endl;
    }
  }
  memset(dp, -1, sizeof(dp));
  // dp[0][0][0][0]=0;
  for (Int i = 0; i < n; i++) {
    for (Int j = i + 1; j < n; j++) {
      dp[i][j][1][(1 << i) | (1 << j)] = 0;
    }
  }
  Int ans = 0;
  for (Int b = 0; b < (1 << n); b++) {
    for (Int i = 0; i < n; i++) {
      for (Int j = i + 1; j < n; j++) {
        for (Int k = 0; k <= n / 2; k++) {
          if (dp[i][j][k][b] < 0)
            continue;
          // cout<<i<<" "<<j<<" "<<k<<" "<<b<<":"<<dp[i][j][k][b]<<endl;
          for (Int s = 0; s < n; s++) {
            for (Int t = s + 1; t < n; t++) {
              if ((b >> s) & 1)
                continue;
              if ((b >> t) & 1)
                continue;
              Int nb = b | (1 << s) | (1 << t);
              if (r[i][j] == r[s][t]) {
                chmax(dp[s][t][k + 1][nb], dp[i][j][k][b] + k);
                chmax(ans, dp[s][t][k + 1][nb]);
              } else {
                chmax(dp[s][t][1][nb], dp[i][j][k][b]);
                chmax(ans, dp[s][t][1][nb]);
              }
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed char dp[16][16][10][1 << 16];
template <typename T1, typename T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  Int n;
  cin >> n;
  using P = pair<Int, Int>;
  vector<P> v;
  for (Int i = 0; i < n; i++) {
    Int x, y;
    cin >> x >> y;
    v.emplace_back(x, y);
  }
  vector<vector<P>> r(n, vector<P>(n));
  for (Int i = 0; i < n; i++) {
    for (Int j = i + 1; j < n; j++) {
      if (v[i].first == v[j].first) {
        r[i][j] = P(0, 1);
        continue;
      }
      if (v[i].second == v[j].second) {
        r[i][j] = P(1, 0);
        continue;
      }
      Int dx = v[i].first - v[j].first;
      Int dy = v[i].second - v[j].second;
      if (dx < 0) {
        dx *= -1;
        dy *= -1;
      }
      Int k = __gcd(dx, abs(dy));
      dx /= k;
      dy /= k;
      r[i][j] = P(dx, dy);
      // cout<<i<<" "<<j<<":"<<dx<<" "<<dy<<endl;
    }
  }
  memset(dp, -1, sizeof(dp));
  // dp[0][0][0][0]=0;
  for (Int i = 0; i < n; i++) {
    for (Int j = i + 1; j < n; j++) {
      dp[i][j][1][(1 << i) | (1 << j)] = 0;
    }
  }
  Int ans = 0;
  for (Int b = 0; b < (1 << n); b++) {
    for (Int i = 0; i < n; i++) {
      for (Int j = i + 1; j < n; j++) {
        for (Int k = 0; k <= n / 2; k++) {
          if (dp[i][j][k][b] < 0)
            continue;
          // cout<<i<<" "<<j<<" "<<k<<" "<<b<<":"<<dp[i][j][k][b]<<endl;
          for (Int s = 0; s < n; s++) {
            for (Int t = s + 1; t < n; t++) {
              if ((b >> s) & 1)
                continue;
              if ((b >> t) & 1)
                continue;
              Int nb = b | (1 << s) | (1 << t);
              if (r[i][j] == r[s][t]) {
                chmax(dp[s][t][k + 1][nb], dp[i][j][k][b] + k);
                chmax(ans, dp[s][t][k + 1][nb]);
              } else {
                chmax(dp[s][t][1][nb], dp[i][j][k][b]);
                chmax(ans, dp[s][t][1][nb]);
              }
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}