Problem p00517 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = std::numeric_limits<long long>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Mfor(i, mf) for (LL i = mf - 1; i >= 0; i--)
LL h, w, n, m, k, s, t, q, sum, last, cnt, ans = 0;
Pll a[110] = {};
LL d[1010][1010] = {};
int dd[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
string str, ss;
struct Edge {
  LL cost, from, to;
};
vector<Edge> vec;
bool f;
char c[10000][10000];
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
int main() {
  cin >> h >> w >> n;
  rep(i, n) {
    cin >> a[i].fir >> a[i].sec;
    if (i) {
      if ((a[i].fir < a[i - 1].fir && a[i].sec > a[i - 1].sec) ||
          (a[i].fir > a[i - 1].fir && a[i].sec < a[i - 1].sec))
        ans += (abs(a[i].fir - a[i - 1].fir) + abs(a[i].sec - a[i - 1].sec));
      else
        ans += max(abs(a[i].fir - a[i - 1].fir), abs(a[i].sec - a[i - 1].sec));
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = std::numeric_limits<long long>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Mfor(i, mf) for (LL i = mf - 1; i >= 0; i--)
LL h, w, n, m, k, s, t, q, sum, last, cnt, ans = 0;
Pll a[1100] = {};
LL d[1010][1010] = {};
int dd[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
string str, ss;
struct Edge {
  LL cost, from, to;
};
vector<Edge> vec;
bool f;
char c[10000][10000];
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
int main() {
  cin >> h >> w >> n;
  rep(i, n) {
    cin >> a[i].fir >> a[i].sec;
    if (i) {
      if ((a[i].fir < a[i - 1].fir && a[i].sec > a[i - 1].sec) ||
          (a[i].fir > a[i - 1].fir && a[i].sec < a[i - 1].sec))
        ans += (abs(a[i].fir - a[i - 1].fir) + abs(a[i].sec - a[i - 1].sec));
      else
        ans += max(abs(a[i].fir - a[i - 1].fir), abs(a[i].sec - a[i - 1].sec));
    }
  }
  cout << ans << endl;
  return 0;
}