Language: C++

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int lim = 1000000001;
int calc(vector<int> v1, vector<int> v2, bool flag_eq = false) {
	if(v1 < v2) return -calc(v2, v1, true) + 1;
	if((v1[1] - v2[1]) % v1[0] != 0) return (v1[1] - v2[1]) / v1[0] + 1;
	int ex = (v1[1] - v2[1]) / v1[0];
	int lp = v1.size();
	for(int i = 0; i < ex; i++) {
		int clp = lp;
		for(int j = 1; j < lp; j++) {
			v2[j] += v2[j - 1];
			if(v2[j] >= lim) {
				v2[j] = lim;
				if(clp == lp) clp = j;
			}
		}
		lp = clp;
		if(lp == 2) {
			v2[1] += ex - i - 1;
			break;
		}
	}
	if((!flag_eq && v1 < v2) || (flag_eq && v1 <= v2)) return ex;
	return ex + 1;
}
int N, M;
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	cin >> N >> M;
	vector<vector<int> > v(N, vector<int>(M));
	for(int i = 0; i < N; i++) {
		for(int j = 0; j < M; j++) {
			cin >> v[i][j];
		}
	}
	if(M == 1) {
		bool f = true;
		for(int i = 1; i < N; i++) {
			if(v[i - 1][0] >= v[i][0]) f = false;
		}
		cout << (f ? 0 : -1) << endl;
	}
	else {
		bool f = true;
		for(int i = 1; i < N; i++) {
			if(v[i - 1][0] > v[i][0]) f = false;
		}
		if(!f) cout << -1 << endl;
		else {
			long long cur = 0, ret = 0;
			for(int i = 1; i < N; i++) {
				long long res = calc(v[i - 1], v[i]);
				cur = max(cur + res, 0);
				ret += cur;
			}
			cout << ret << endl;
		}
	}
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:57:42: error: no matching function for call to 'max(long long int, int)'
   57 |                                 cur = max(cur + res, 0);
      |                                       ~~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:62,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:57:42: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   57 |                                 cur = max(cur + res, 0);
      |                                       ~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:57:42: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   57 |                                 cur = max(cur + res, 0);
      |                                       ~~~^~~~~~~~~~~~~~