Problem p03902 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 *    author:  tourist
 *    created: 26.11.2019 09:33:48
 **/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < n - 1; i++) {
    if (a[i][0] > a[i + 1][0] || (m == 1 && a[i][0] == a[i + 1][0])) {
      cout << -1 << '\n';
      return 0;
    }
  }
  if (m == 1) {
    cout << 0 << '\n';
    return 0;
  }
  const long long inf = (long long)1.01e9;
  auto Transform = [&](vector<long long> &v, long long x) {
    for (int i = m - 1; i >= 1; i--) {
      long long c = 1;
      for (int j = 0; j < i; j++) {
        c *= x + j;
        c /= j + 1;
        v[i] = (c > inf ? inf : min(v[i] + v[i - 1 - j] * c, inf));
        if (v[i] == inf) {
          break;
        }
      }
    }
  };
  auto Compare = [&](int i, long long p, long long q) {
    vector<long long> b(a[i].begin(), a[i].end());
    vector<long long> c(a[i + 1].begin(), a[i + 1].end());
    Transform(b, p - min(p, q));
    Transform(c, q - min(p, q));
    return (b == c ? 0 : (b > c ? 1 : -1));
  };
  long long ans = 0;
  long long last = 0;
  for (int i = 1; i < n; i++) {
    if (a[i][0] > a[i - 1][0]) {
      last = 0;
      continue;
    }
    long long val = a[i - 1][1] + a[i - 1][0] * last;
    long long steps = 0;
    if (val > a[i][1]) {
      steps = (val - a[i][1] + a[i][0] - 1) / a[i][0];
    }
    if (a[i][1] + a[i][0] * steps == val) {
      if (Compare(i - 1, last, steps) >= 0) {
        ++steps;
      }
    }
    last = steps;
    ans += last;
  }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
/**
 *    author:  tourist
 *    created: 26.11.2019 09:33:48
 **/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < n - 1; i++) {
    if (a[i][0] > a[i + 1][0] || (m == 1 && a[i][0] == a[i + 1][0])) {
      cout << -1 << '\n';
      return 0;
    }
  }
  if (m == 1) {
    cout << 0 << '\n';
    return 0;
  }
  const long long inf = (long long)1.01e9;
  auto Transform = [&](vector<long long> &v, long long x) {
    if (x < 42) {
      while (x-- > 0) {
        for (int j = 1; j < m; j++) {
          v[j] = min(v[j] + v[j - 1], inf);
        }
      }
    } else {
      for (int i = m - 1; i >= 1; i--) {
        long long c = 1;
        for (int j = 0; j < i; j++) {
          c *= x + j;
          c /= j + 1;
          v[i] = (c > inf ? inf : min(v[i] + v[i - 1 - j] * c, inf));
          if (v[i] == inf) {
            break;
          }
        }
      }
    }
  };
  auto Compare = [&](int i, long long p, long long q) {
    vector<long long> b(a[i].begin(), a[i].end());
    vector<long long> c(a[i + 1].begin(), a[i + 1].end());
    Transform(b, p - min(p, q));
    Transform(c, q - min(p, q));
    return (b == c ? 0 : (b > c ? 1 : -1));
  };
  long long ans = 0;
  long long last = 0;
  for (int i = 1; i < n; i++) {
    if (a[i][0] > a[i - 1][0]) {
      last = 0;
      continue;
    }
    long long val = a[i - 1][1] + a[i - 1][0] * last;
    long long steps = 0;
    if (val > a[i][1]) {
      steps = (val - a[i][1] + a[i][0] - 1) / a[i][0];
    }
    if (a[i][1] + a[i][0] * steps == val) {
      if (Compare(i - 1, last, steps) >= 0) {
        ++steps;
      }
    }
    last = steps;
    ans += last;
  }
  cout << ans << '\n';
  return 0;
}