Problem p01733 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
// #define mt make_tuple
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
// typedef tuple<int,int,int> tp;
// typedef vector<tp> vt;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e9;
const ll INF = 2e18;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
map<ll, int> ma;
vvi in;
int main() {
  int n;
  cin >> n;
  in = vvi(n);
  rep(i, n) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    in[i] = vi{a, b, c};
    ma[a * 1000000001ll + b] = c;
  }
  ll a = 0, b = 1, sum, sx, sy, gx, gy, area, gc_d = 1;
  rep(q, n) {
    loop(x, -4, 5) loop(y, -4, 5) if (x != 0 && y != 0) {
      sum = 0;
      sx = min(in[q][0], in[q][0] + x);
      gx = max(in[q][0], in[q][0] + x);
      sy = min(in[q][1], in[q][1] + y);
      gy = max(in[q][1], in[q][1] + y);
      loop(i, sx, gx + 1) loop(j, sy, gy + 1) sum += ma[i * 1000000001ll + j];
      area = abs(x * y);
      gc_d = gcd(sum, area);
      sum /= gc_d;
      area /= gc_d;
      if (a * area < b * sum) {
        a = sum;
        b = area;
      }
    }
  }
  cout << a << " / " << b << endl;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
// #define mt make_tuple
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
// typedef tuple<int,int,int> tp;
// typedef vector<tp> vt;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e9;
const ll INF = 2e18;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
map<ll, int> ma;
vvi in;
int main() {
  int n;
  cin >> n;
  in = vvi(n);
  rep(i, n) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    in[i] = vi{a, b, c};
    ma[a * 1000000001ll + b] = c;
  }
  ll a = 0, b = 1, sum, sx, sy, gx, gy, area, gc_d = 1;
  rep(q, n) {
    loop(x, -4, 5) loop(y, -4, 5) if (x != 0 && y != 0) {
      sum = 0;
      sx = min(in[q][0], in[q][0] + x);
      gx = max(in[q][0], in[q][0] + x);
      sy = min(in[q][1], in[q][1] + y);
      gy = max(in[q][1], in[q][1] + y);
      loop(i, sx, gx + 1)
          loop(j, sy, gy + 1) if (ma.count(i * 1000000001ll + j)) sum +=
          ma[i * 1000000001ll + j];
      area = abs(x * y);
      gc_d = gcd(sum, area);
      sum /= gc_d;
      area /= gc_d;
      if (a * area < b * sum) {
        a = sum;
        b = area;
      }
    }
  }
  cout << a << " / " << b << endl;
}