Problem p02556 (C++) — buggy submission, verdict: Time Limit Exceeded

// Created by Kshitij Anand NSIT
#include <bits/stdc++.h>
// #include <ext/numeric>
// using namespace __gnu_cxx;

using namespace std;

#define int long long
#define pb push_back
#define P pair<int, int>
#define F first
#define S second
#define vi vector<int>
#define vc vector<char>
#define vb vector<bool>
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mp(a, b) make_pair(a, b)
#define min3(a, b, c) min(min(a, b), c)
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define fill(arr, val) memset(arr, val, sizeof(arr))
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define db(x) cout << #x << " : " << x << endl

const int N = 1000000007;

bool comp(P a, P b) {

  int val1 = a.F + a.S;
  int val2 = b.F + b.S;

  if (val1 < val2)
    return true;
  // if(val1 < val2)return a;
  return false;

  // return
}

bool comp2(P a, P b) {

  int val1 = a.F - a.S;
  int val2 = b.F - b.S;

  if (val1 < val2)
    return true;
  // if(val1 < val2)return a;
  return false;

  // return
}

void solve() {

  int n, a, b, c, ans = 0, count = 0, sum = 0;
  cin >> n;
  P arr[n];

  for (int i = 0; i < n; i++) {
    cin >> arr[i].F >> arr[i].S;
  }

  sort(arr, arr + n, comp);
  ans = max(ans, arr[n - 1].F + arr[n - 1].S - arr[0].F - arr[0].S);
  // for(int i=0; i<n - 1; i++) {
  // }
  sort(arr, arr + n, comp2);
  ans = max(ans, arr[n - 1].F - arr[n - 1].S - arr[0].F + arr[0].S);

  // for(int i=1; i<n; i++) {
  //     ans = max(ans ,abs(arr[i].F - arr[0].F) + abs(arr[i].S - arr[0].S) );
  // }

  // ans = abs(arr[0].F - arr[n - 1].F) + abs(arr[0].S - arr[n - 1].S);

  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  //  int t; cin>>t; while(t--)
  { solve(); }

  return 0;
}


Error: TLE

Fixed submission:

// Created by Kshitij Anand NSIT
#include <bits/stdc++.h>
// #include <ext/numeric>
// using namespace __gnu_cxx;

using namespace std;

#define int long long
#define pb push_back
#define P pair<int, int>
#define F first
#define S second
#define vi vector<int>
#define vc vector<char>
#define vb vector<bool>
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mp(a, b) make_pair(a, b)
#define min3(a, b, c) min(min(a, b), c)
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define fill(arr, val) memset(arr, val, sizeof(arr))
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define db(x) cout << #x << " : " << x << endl

const int N = 1000000007;

bool comp(P a, P b) {

  int val1 = a.F + a.S;
  int val2 = b.F + b.S;

  if (val1 < val2)
    return true;
  // if(val1 < val2)return a;
  return false;

  // return
}

void solve() {

  int n, a, b, c, ans = 0, count = 0, sum = 0;
  cin >> n;
  P arr[n];

  for (int i = 0; i < n; i++) {
    cin >> arr[i].F >> arr[i].S;
  }

  sort(arr, arr + n, comp);
  // for(int i=0; i<n - 1; i++) {
  // }

  for (int i = 0; i < n; i++) {

    for (int j = i + 1; j < n; j++) {

      ans = max(ans, abs(arr[i].F - arr[j].F) + abs(arr[i].S - arr[j].S));
    }
  }

  // for(int i=1; i<n; i++) {
  //     ans = max(ans ,abs(arr[i].F - arr[0].F) + abs(arr[i].S - arr[0].S) );
  // }

  // ans = abs(arr[0].F - arr[n - 1].F) + abs(arr[0].S - arr[n - 1].S);

  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  //  int t; cin>>t; while(t--)
  { solve(); }

  return 0;
}