Problem p03326 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define rep(i, s, n) for (i64 i = (s), i##_limit = (n); i < i##_limit; ++i)
#define repr(i, s, t) for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#define VAR(type, ...)                                                         \
  type __VA_ARGS__;                                                            \
  read(__VA_ARGS__);
#ifndef DBG
#define dump(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  return b > a && (a = b, true);
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  return b < a && (a = b, true);
}
constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T> inline vector<T> makeV(const T &initValue, size_t sz) {
  return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto makeV(const T &initValue, size_t sz, Args... args) {
  return vector<decltype(makeV<T>(initValue, args...))>(
      sz, makeV<T>(initValue, args...));
}

template <class T> inline istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &e : vec)
    is >> e;
  return is;
}

inline void read() {}

template <class Head, class... Tail>
inline void read(Head &head, Tail &...tail) {
  cin >> head;
  read(tail...);
}

inline void print() { cout << "\n"; }

template <class Head, class... Tail>
inline void print(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail))
    cout << ' ';
  print(forward<Tail>(tail)...);
}

template <class T>
inline ostream &operator<<(ostream &os, const vector<T> &vec) {
  static constexpr const char *delim[] = {" ", ""};
  for (const auto &e : vec)
    os << e << delim[&e == &vec.back()];
  return os;
}
// }}} End Header

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  VAR(int, N, M);
  vector<i64> a(N), b(N), c(N);
  vector<int> id(N);

  rep(i, 0, N) {
    read(a[i], b[i], c[i]);
    id[i] = i;
  }

  constexpr int S[] = {1, -1};
  i64 ans = 0;
  for (const int x : S)
    for (const int y : S)
      for (const int z : S) {
        sort(all(id), [&](int i, int j) {
          return a[i] * x + b[i] * y + c[i] * z >
                 a[j] * j + b[j] * y + c[j] * z;
        });

        i64 sumA = 0;
        i64 sumB = 0;
        i64 sumC = 0;
        rep(i, 0, M) {
          sumA += a[id[i]];
          sumB += b[id[i]];
          sumC += c[id[i]];
        }
        chmax(ans, abs(sumA) + abs(sumB) + abs(sumC));
      }

  print(ans);

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define rep(i, s, n) for (i64 i = (s), i##_limit = (n); i < i##_limit; ++i)
#define repr(i, s, t) for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#define VAR(type, ...)                                                         \
  type __VA_ARGS__;                                                            \
  read(__VA_ARGS__);
#ifndef DBG
#define dump(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  return b > a && (a = b, true);
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  return b < a && (a = b, true);
}
constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T> inline vector<T> makeV(const T &initValue, size_t sz) {
  return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto makeV(const T &initValue, size_t sz, Args... args) {
  return vector<decltype(makeV<T>(initValue, args...))>(
      sz, makeV<T>(initValue, args...));
}

template <class T> inline istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &e : vec)
    is >> e;
  return is;
}

inline void read() {}

template <class Head, class... Tail>
inline void read(Head &head, Tail &...tail) {
  cin >> head;
  read(tail...);
}

inline void print() { cout << "\n"; }

template <class Head, class... Tail>
inline void print(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail))
    cout << ' ';
  print(forward<Tail>(tail)...);
}

template <class T>
inline ostream &operator<<(ostream &os, const vector<T> &vec) {
  static constexpr const char *delim[] = {" ", ""};
  for (const auto &e : vec)
    os << e << delim[&e == &vec.back()];
  return os;
}
// }}} End Header

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  VAR(int, N, M);
  vector<i64> a(N), b(N), c(N);
  vector<int> id(N);

  rep(i, 0, N) {
    read(a[i], b[i], c[i]);
    id[i] = i;
  }

  constexpr int S[] = {1, -1};
  i64 ans = 0;
  for (const int x : S)
    for (const int y : S)
      for (const int z : S) {
        sort(all(id), [&](int i, int j) {
          return a[i] * x + b[i] * y + c[i] * z >
                 a[j] * x + b[j] * y + c[j] * z;
        });

        i64 sumA = 0;
        i64 sumB = 0;
        i64 sumC = 0;
        rep(i, 0, M) {
          sumA += a[id[i]];
          sumB += b[id[i]];
          sumC += c[id[i]];
        }
        chmax(ans, abs(sumA) + abs(sumB) + abs(sumC));
      }

  print(ans);

  return 0;
}