Problem p03326 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

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;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
  int val = ((a % mod) + (b % mod)) % mod;
  if (val < 0) {
    val += mod;
  }
  a = val;
}
////////////////////////////////////////

int main() {
  ll N, M, a[100009][3], maxn = -(1LL << 60);
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < 8; ++i) {
    vector<ll> vec;
    for (int j = 0; j < N; ++j) {
      ll S = 0;
      for (int k = 2; k >= 0; ++k) {
        if ((i / (1 << k)) % 2 == 0) {
          S += a[j][k];
        } else {
          S -= a[j][k];
        }
      }
      vec.push_back(S);
    }
    sort(vec.begin(), vec.end());
    reverse(vec.begin(), vec.end());
    ll ans = 0;
    for (int j = 0; j < M; ++j) {
      ans += vec[j];
    }
    maxn = max(maxn, ans);
  }
  cout << maxn << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

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;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
  int val = ((a % mod) + (b % mod)) % mod;
  if (val < 0) {
    val += mod;
  }
  a = val;
}
////////////////////////////////////////

int main() {
  ll N, M, a[100009][3], maxn = -(1LL << 60);
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < 8; ++i) {
    vector<ll> vec;
    for (int j = 0; j < N; ++j) {
      ll S = 0;
      for (int k = 0; k < 3; ++k) {
        if ((i / (1 << k)) % 2 == 0) {
          S += a[j][k];
        } else {
          S -= a[j][k];
        }
      }
      vec.push_back(S);
    }
    sort(vec.begin(), vec.end());
    reverse(vec.begin(), vec.end());
    ll ans = 0;
    for (int j = 0; j < M; ++j) {
      ans += vec[j];
    }
    maxn = max(maxn, ans);
  }
  cout << maxn << endl;
  return 0;
}