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

/*
AUTHOR: Aman Raj
LANG: C++
*/

using namespace std;

typedef long long int ll;
#include <bits/stdc++.h>

// setbase - cout << setbase (16); cout << 100 << endl; Prints 64
// setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prints
// xxx77 setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
// cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(a) a.begin(), a.end()
// #define mod 998244353

#define mod 1000000007

#define inf 1e18
// #define mod 3
#define flush fflush(stdout)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef cc_hash_table<ll, ll, hash<ll>> ht;

// find_by_order()  // order_of_key
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

ll power1(ll a, ll b) // a raised to power b
{
  if (b <= 0)
    return 1;
  if (b == 1)
    return a;
  ll c = power1(a, b / 2);
  c *= c;
  c %= mod;
  if (b % 2 == 1)
    c *= a;
  c %= mod;
  return c;
}
ll mi(ll a, ll m) {
  ll c = power1(a, m - 2);

  return c;
}

struct less_then_greater {
  template <typename T, typename U>
  bool operator()(T const &lhs, U const &rhs) const {
    if (lhs.first < rhs.first)
      return true;
    if (rhs.first < lhs.first)
      return false;
    return lhs.second > rhs.second;
  }
};

/*std::priority_queue<std::pair<int, int>,
                   std::vector<std::pair<int, int>>,
                   less_then_greater
                   > pq;*/

bool comp1(pair<pair<ll, ll>, ll> p1, pair<pair<ll, ll>, ll> p2) {
  if (p1.first.second != p2.first.second)
    return p1.first.second <= p2.first.second;
  if (p1.first.first != p2.first.first)
    return p1.first.first <= p2.first.first;
  return p1.second < p2.second;
}
bool comp2(pair<pair<ll, ll>, ll> p1, pair<pair<ll, ll>, ll> p2) {
  if (p1.first.second != p2.first.second)
    return p1.first.second <= p2.first.second;
  if (p1.first.first != p2.first.first)
    return p1.first.first >= p2.first.first;
  return p1.second < p2.second;
}
bool comp3(pair<pair<ll, ll>, ll> p1, pair<pair<ll, ll>, ll> p2) {
  if (p1.first.first != p2.first.first)
    return p1.first.first <= p2.first.first;
  if (p1.first.second != p2.first.second)
    return p1.first.second >= p2.first.second;
  return p1.second < p2.second;
}
bool comp4(pair<pair<ll, ll>, ll> p1, pair<pair<ll, ll>, ll> p2) {
  if (p1.first.first != p2.first.first)
    return p1.first.first >= p2.first.first;
  if (p1.first.second != p2.first.second)
    return p1.first.second >= p2.first.second;
  return p1.second < p2.second;
}
int main() {
  fast;
  ll n;
  cin >> n;
  vector<pair<pair<ll, ll>, ll>> v(n);
  ll i = 0;
  for (auto &x : v) {
    cin >> x.first.first >> x.first.second;
    x.second = i;
    i++;
  }
  ll ans = 0;
  // first move on increasing y and then decreasing y

  sort(all(v), comp1); // increasig x and increasing y
  vector<ll> dp(n + 1);
  dp[n - 1] = v.back().first.first + v.back().first.second;
  for (ll i = n - 2; i >= 0; i--)
    dp[i] = max(dp[i + 1], v[i].first.first + v[i].first.second);

  for (ll i = 0; i < n - 1; i++) {
    ans = max(ans, dp[i + 1] - v[i].first.first - v[i].first.second);
  }

  sort(all(v), comp2); // increasing y and decreasing x
  dp.resize(n + 1);
  dp[n - 1] = v.back().first.second - v.back().first.first;
  for (ll i = n - 2; i >= 0; i--)
    dp[i] = max(dp[i + 1], -v[i].first.first + v[i].first.second);

  for (ll i = 0; i < n - 1; i++) {
    ans = max(ans, dp[i + 1] + v[i].first.first - v[i].first.second);
  }

  sort(all(v), comp3); // increasing x decreasing y
  dp.resize(n + 1);
  dp[n - 1] = -v.back().first.second + v.back().first.first;
  for (ll i = n - 2; i >= 0; i--)
    dp[i] = max(dp[i + 1], v[i].first.first - v[i].first.second);

  for (ll i = 0; i < n - 1; i++) {
    ans = max(ans, dp[i + 1] - v[i].first.first + v[i].first.second);
  }

  sort(all(v), comp4); // decreasing x decreasing y
  dp.resize(n + 1);
  dp[n - 1] = -v.back().first.second - v.back().first.first;
  for (ll i = n - 2; i >= 0; i--)
    dp[i] = max(dp[i + 1], -v[i].first.first - v[i].first.second);

  for (ll i = 0; i < n - 1; i++) {
    ans = max(ans, dp[i + 1] + v[i].first.first + v[i].first.second);
  }

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

/*
AUTHOR: Aman Raj
LANG: C++
*/

using namespace std;

typedef long long int ll;
#include <bits/stdc++.h>

// setbase - cout << setbase (16); cout << 100 << endl; Prints 64
// setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prints
// xxx77 setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
// cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(a) a.begin(), a.end()
// #define mod 998244353

#define mod 1000000007

#define inf 1e18
// #define mod 3
#define flush fflush(stdout)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef cc_hash_table<ll, ll, hash<ll>> ht;

// find_by_order()  // order_of_key
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

ll power1(ll a, ll b) // a raised to power b
{
  if (b <= 0)
    return 1;
  if (b == 1)
    return a;
  ll c = power1(a, b / 2);
  c *= c;
  c %= mod;
  if (b % 2 == 1)
    c *= a;
  c %= mod;
  return c;
}
ll mi(ll a, ll m) {
  ll c = power1(a, m - 2);

  return c;
}

struct less_then_greater {
  template <typename T, typename U>
  bool operator()(T const &lhs, U const &rhs) const {
    if (lhs.first < rhs.first)
      return true;
    if (rhs.first < lhs.first)
      return false;
    return lhs.second > rhs.second;
  }
};

/*std::priority_queue<std::pair<int, int>,
                   std::vector<std::pair<int, int>>,
                   less_then_greater
                   > pq;*/

bool comp1(pair<pair<ll, ll>, ll> p1, pair<pair<ll, ll>, ll> p2) {
  if (p1.first.second != p2.first.second)
    return p1.first.second <= p2.first.second;
  if (p1.first.first != p2.first.first)
    return p1.first.first <= p2.first.first;
  return p1.second < p2.second;
}
bool comp2(pair<pair<ll, ll>, ll> p1, pair<pair<ll, ll>, ll> p2) {
  if (p1.first.second != p2.first.second)
    return p1.first.second <= p2.first.second;
  if (p1.first.first != p2.first.first)
    return p1.first.first >= p2.first.first;
  return p1.second < p2.second;
}
bool comp3(pair<pair<ll, ll>, ll> p1, pair<pair<ll, ll>, ll> p2) {
  if (p1.first != p2.first)
    return p1.first.first <= p2.first.first;
  if (p1.first.second != p2.first.second)
    return p1.first.second >= p2.first.second;
  return p1.second < p2.second;
}
bool comp4(pair<pair<ll, ll>, ll> p1, pair<pair<ll, ll>, ll> p2) {
  if (p1.first != p2.first)
    return p1.first.first >= p2.first.first;
  if (p1.first.second != p2.first.second)
    return p1.first.second >= p2.first.second;
  return p1.second < p2.second;
}
int main() {
  fast;
  ll n;
  cin >> n;
  vector<pair<pair<ll, ll>, ll>> v(n);
  ll i = 0;
  for (auto &x : v) {
    cin >> x.first.first >> x.first.second;
    x.second = i;
    i++;
  }
  ll ans = 0;
  // first move on increasing y and then decreasing y

  sort(all(v), comp1); // increasig x and increasing y
  vector<ll> dp(n + 1);
  dp[n - 1] = v.back().first.first + v.back().first.second;
  for (ll i = n - 2; i >= 0; i--)
    dp[i] = max(dp[i + 1], v[i].first.first + v[i].first.second);

  for (ll i = 0; i < n - 1; i++) {
    ans = max(ans, dp[i + 1] - v[i].first.first - v[i].first.second);
  }

  sort(all(v), comp2); // increasing y and decreasing x
  dp.resize(n + 1);
  dp[n - 1] = v.back().first.second - v.back().first.first;
  for (ll i = n - 2; i >= 0; i--)
    dp[i] = max(dp[i + 1], -v[i].first.first + v[i].first.second);

  for (ll i = 0; i < n - 1; i++) {
    ans = max(ans, dp[i + 1] + v[i].first.first - v[i].first.second);
  }

  sort(all(v), comp3); // increasing x decreasing y
  dp.resize(n + 1);
  dp[n - 1] = -v.back().first.second + v.back().first.first;
  for (ll i = n - 2; i >= 0; i--)
    dp[i] = max(dp[i + 1], v[i].first.first - v[i].first.second);

  for (ll i = 0; i < n - 1; i++) {
    ans = max(ans, dp[i + 1] - v[i].first.first + v[i].first.second);
  }

  sort(all(v), comp4); // decreasing x decreasing y
  dp.resize(n + 1);
  dp[n - 1] = -v.back().first.second - v.back().first.first;
  for (ll i = n - 2; i >= 0; i--)
    dp[i] = max(dp[i + 1], -v[i].first.first - v[i].first.second);

  for (ll i = 0; i < n - 1; i++) {
    ans = max(ans, dp[i + 1] + v[i].first.first + v[i].first.second);
  }

  cout << ans << endl;
  return 0;
}