Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vlli vector<ll>
#define vi vector<ll>
#define vs vector<string>
#define vplli vector<pair<ll, ll>>
#define plli pair<ll, ll>
#define vps vector<pair<string, string>>
#define vpi vector<pair<ll, ll>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define forn(i, a, n) for (ll i = a; i < n; i++)
#define forr(i, n, a) for (ll i = n - 1; i >= a; i--)
#define scan(arr, a, n)                                                        \\
  for (ll i = (a); i < (n); i++)                                               \\
    cin >> (arr)[i];
#define prll(arr, a, n)                                                        \\
  for (ll i = (a); i < (n); i++)                                               \\
    cout << (arr)[i] << " ";

const ll inf = 1e18;

ll add(ll x, ll y) {
  ll res = x + y;
  return (res >= mod ? res - mod : res);
}
ll mul(ll x, ll y) {
  ll res = x * y;
  return (res >= mod ? res % mod : res);
}
ll sub(ll x, ll y) {
  ll res = x - y;
  return (res < 0 ? res + mod : res);
}
ll power(ll x, ll y) {
  ll res = 1;
  x %= mod;
  while (y) {

    if (y & 1)
      res = mul(res, x);
    y >>= 1;
    x = mul(x, x);
  }
  return res;
}
ll mod_inv(ll x) { return power(x, mod - 2); }

int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  ll n, m, q;
  cin >> n >> m >> q;
  ll x, y, i, j;
  map<ll, ll> ma[n];
  vplli a(n);
  vplli b(m);
  for (i = 0; i < n; i++) {
    a[i].F = 0;
    a[i].S = i;
  }
  for (i = 0; i < m; i++) {
    b[i].F = 0;
    b[i].S = i;
  }

  while (q--) {
    cin >> x >> y;
    a[x - 1].F++;
    b[y - 1].F++;
    ma[x - 1][y - 1]++;
  }
  ll mx = 0;
  sort(all(a));
  sort(all(b));
  for (i = n - 1; i >= 0; i--) {
    for (j = m - 1; j >= 0; j--) {
      x = b[j].F + a[i].F;
      if (x <= mx)
        break;
      if (ma[a[i].S][b[j].S])
        x--;
      mx = max(x, mx);
    }
    if (a[i].F + b[m - 1].F <= mx)
      break;
  }

  cout << mx << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vlli vector<ll>
#define vi vector<ll>
#define vs vector<string>
#define vplli vector<pair<ll, ll>>
#define plli pair<ll, ll>
#define vps vector<pair<string, string>>
#define vpi vector<pair<ll, ll>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define forn(i, a, n) for (ll i = a; i < n; i++)
#define forr(i, n, a) for (ll i = n - 1; i >= a; i--)
#define scan(arr, a, n)                                                        \\
  for (ll i = (a); i < (n); i++)                                               \\
    cin >> (arr)[i];
#define prll(arr, a, n)                                                        \\
  for (ll i = (a); i < (n); i++)                                               \\
    cout << (arr)[i] << " ";

const ll inf = 1e18;

ll add(ll x, ll y) {
  ll res = x + y;
  return (res >= mod ? res - mod : res);
}
ll mul(ll x, ll y) {
  ll res = x * y;
  return (res >= mod ? res % mod : res);
}
ll sub(ll x, ll y) {
  ll res = x - y;
  return (res < 0 ? res + mod : res);
}
ll power(ll x, ll y) {
  ll res = 1;
  x %= mod;
  while (y) {

    if (y & 1)
      res = mul(res, x);
    y >>= 1;
    x = mul(x, x);
  }
  return res;
}
ll mod_inv(ll x) { return power(x, mod - 2); }

int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  ll h, w, m;
  cin >> h >> w >> m;
  ll x, y, i, j;
  map<ll, ll> ma[h];
  vplli a(h);
  vplli b(w);
  for (i = 0; i < h; i++) {
    a[i].F = 0;
    a[i].S = i;
  }
  for (i = 0; i < m; i++) {
    b[i] = mp(0, i);
  }

  while (m--) {
    cin >> x >> y;
    a[x - 1].F++;
    b[y - 1].F++;
    ma[x - 1][y - 1]++;
  }
  ll mx = 0;
  sort(all(a));
  sort(all(b));
  for (i = h - 1; i >= 0; i--) {
    for (j = w - 1; j >= 0; j--) {
      x = b[j].F + a[i].F;
      if (x <= mx)
        break;
      if (ma[a[i].S][b[j].F])
        x--;
      mx = max(x, mx);
    }
    if (a[i].F + b[m - 1].F <= mx)
      break;
  }

  cout << mx << endl;
}