Problem:
Estimating the Flood Risk
Mr. Boat is the owner of a vast extent of land. As many typhoons have struck Japan this year, he became concerned of flood risk of his estate and he wants to know the average altitude of his land. The land is too vast to measure the altitude at many spots. As no steep slopes are in the estate, he thought that it would be enough to measure the altitudes at only a limited number of sites and then approximate the altitudes of the rest based on them.
Multiple approximations might be possible based on the same measurement results, in which case he wants to know the worst case, that is, one giving the lowest average altitude.
Mr. Boat’s estate, which has a rectangular shape, is divided into grid-aligned rectangular areas of the same size. Altitude measurements have been carried out in some of these areas, and the measurement results are now at hand. The altitudes of the remaining areas are to be approximated on the assumption that altitudes of two adjoining areas sharing an edge differ at most 1.
In the first sample given below, the land is divided into 5 × 4 areas. The altitudes of the areas at (1, 1) and (5, 4) are measured 10 and 3, respectively. In this case, the altitudes of all the areas are uniquely determined on the assumption that altitudes of adjoining areas differ at most 1.
In the second sample, there are multiple possibilities, among which one that gives the lowest average altitude should be considered.
In the third sample, no altitude assignments satisfy the assumption on altitude differences.
Your job is to write a program that approximates the average altitude of his estate. To be precise, the program should compute the total of approximated and measured altitudes of all the mesh-divided areas. If two or more different approximations are possible, the program should compute the total with the severest approximation, that is, one giving the lowest total of the altitudes.
Input
The input consists of a single test case of the following format.
$w$ $d$ $n$
$x_1$ $y_1$ $z_1$
.
.
.
$x_n$ $y_n$ $z_n$
Here, $w$, $d$, and $n$ are integers between $1$ and $50$, inclusive. $w$ and $d$ are the numbers of areas in the two sides of the land. $n$ is the number of areas where altitudes are measured. The $i$-th line of the following $n$ lines contains three integers, $x_i$, $y_i$, and $z_i$ satisfying $1 \leq x_i \leq w$, $1 \leq y_i \leq d$, and $−100 \leq  z_i \leq 100$. They mean that the altitude of the area at $(x_i
, y_i)$ was measured to be $z_i$. At most one measurement result is given for the same area, i.e., for $i \ne j$, $(x_i, y_i) \ne (x_j , y_j)$.
Output
If all the unmeasured areas can be assigned their altitudes without any conflicts with the measured altitudes assuming that two adjoining areas have the altitude difference of at most 1, output an integer that is the
total
of the measured or approximated altitudes of all the areas. If more than one such altitude assignment is possible, output the minimum altitude
...(truncated)

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)
#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}

int dx[] = {-1,0,0,1};
int dy[] = {0,-1,1,0};

int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  int w,d,n;cin >> w >> d >> n;
  vector<tuple<int,int,int>> v;
  rep(i,0,n){
    int x,y,z;cin >> x >> y >> z;
    x--;y--;
    v.emplace_back(w*d,x*d + y,-z);
    v.emplace_back(x*d + y,w*d,z);
  }
  rep(i,0,w)rep(j,0,d)rep(k,0,4){
    int ni = i + dy[k],nj = j + dx[k];
    if(ni < 0 || ni >= w || nj < 0 || nj >= d)continue;
    v.emplace_back(i*d+j,ni*d+nj,1);
  }
  const ll inf = 1ll << 60;
  vi D(w*d+1,inf);
  D[w*d] = 0;
  rep(_,0,w*d+1){
    bool is = false;
    rep(i,0,sz(v)){
      auto [fr,to,we] = v[i];
      if(chmin(D[to],D[fr]+we)){
        is = true;
      }
    }
    if(_ == w*d && is){
      cout << "No" << endl;
      return 0;
    }
  }
  ll res = 0;
  rep(i,0,w*d)res -= D[i] - D[w*d];
  cout << res << endl;
  

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3608
final_rank: 6

code_snippet: #include <bits/stdc++.h>
int W, D, N, X[50], Y[50], Z[50];
void solve() {
    bool ok = true;
    for (int i = 0 ; i < N ; i++) {
        for (int j = i + 1 ; j < N ; j++) {
            int d = std::abs(X[i] - X[j]) + std::abs(Y[i] - Y[j]);
            ok &= d >= std::abs(Z[i] - Z[j]);
        }
    }
    if (!ok) {
        std::cout << "No\n";
        return;
    }
    int ans = 0;
    for (int i = 0 ; i < W ; i++) for (int j = 0 ; j < D ; j++) {
        bool fn = false;
        for (int v = -300 ; v <= 300 and !fn ; v++) {
            bool can = true;
            for (int k = 0 ; k < N ; k++) {
                int d = std::abs(X[k] - i) + std::abs(Y[k] - j);
                can &= d >= std::abs(Z[k] - v);
            }
            if (can) {
                fn = true;
                ans += v;
                break;
            }
        }
        assert(fn);
    }
    std::cout << ans << '\n';
}
int main() {
    std::cin >> W >> D >> N;
    for (int i = 0 ; i < N ; i++) {
        std::cin >> X[i] >> Y[i] >> Z[i];
        X[i]--; Y[i]--;
    }
    solve();
}
accuracy: 1
time_ms: 60
memory_kb: 3444
final_rank: 5

code_snippet: //Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <iomanip>
#include <chrono>
#include <random>
#include <bitset>
#include <complex>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#define int long long
//#define L __int128
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define fi first
#define sc second
//#define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) int(x.size())
#define pcnt(x) __builtin_popcountll(x)

#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
 
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
 
template<class t> using vc=vector<t>;
 
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.fi<<","<<p.sc<<"}";
}
 
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}
 
 //https://codeforces.com/blog/entry/62393
struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
 
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
	//don't make x negative!
	size_t operator()(pair<int,int> x)const{
		return operator()(uint64_t(x.first)<<32|x.second);
	}
};
//unordered_set -> dtype, null_type
//unordered_map -> dtype(key), dtype(value)
using namespace __gnu_pbds;
template<class t,class u>
using hash_table=gp_hash_table<t,u,custom_hash>;

template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 998244353;
//const ll mod = 1000000007;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}
ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}
#define _sz 1
ll F[_sz],R[_sz];
void make(){
	F[0] = 1;
	for(int i=1;i<_sz;i++) F[i] = F[i-1]*i%mod;
	R[_sz-1] = modpow(F[_sz-1], mod-2);
	for(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1) % mod;
}
ll C(int a,int b){
	if(b < 0 || a < b) return 0;
	return F[a]*R[b]%mod*R[a-b]%mod;
}
int mx[55][55], mn[55][55];
int w,d,n;
void solve(){
	cin>>w>>d>>n;
	rep(i,55)rep(j,55){
		mx[i][j]=INF;
		mn[i][j]=-INF;
	}
	rep(i, n){
		int x, y, z; cin>>x>>y>>z;
		mx[x][y] = z;
		mn[x][y] = z;
	}
	
	priority_queue<P1>que;
	repn(i,w)repn(j,d) if(mx[i][j] < INF) que.emplace(mx[i][j], mp(i, j));
	int dx[4]={0,1,0,-1};
	int dy[4]={1,0,-1,0};
	while(!que.empty()){
		auto q = que.top(); que.pop();//o(q);
		int v = q.a;
		int x = q.b.a;
		int y = q.b.b;
		if(mx[x][y] != v) continue;
		rep(k, 4){
			int nx = x + dx[k];
			int ny = y + dy[k];
			if(1 <= nx and nx <= w and 1 <= ny and ny <= d and mx[nx][ny] > v+1){
				mx[nx][ny] = v+1;
				que.emplace(mx[nx][ny], mp(nx, ny));
			}
		}
	}
	repn(i,w)repn(j,d) if(mn[i][j] > -INF) que.emplace(-mn[i][j], mp(i, j));
	while(!que.empty()){
		auto q = que.top(); que.pop();
		int v = q.a;
		int x = q.b.a;
		int y = q.b.b;
		if(mn[x][y] != -v) continue;
		rep(k, 4){
			int nx = x + dx[k];
			int ny = y + dy[k];
			if(1 <= nx and nx <= w and 1 <= ny and ny <= d and mn[nx][ny] < -v-1){
				mn[nx][ny] = -v-1;
				que.emplace(-mn[nx][ny], mp(nx, ny));
			}
		}
	}
	int ans = 0;
	repn(i, w) repn(j, d){
		if(mn[i][j] > mx[i][j]){
			o("No");
			return ;
		}
		ans += mn[i][j];
	}
	o(ans);
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	int t; t = 1; //cin >> t;
	while(t--) solve();
}
accuracy: 1
time_ms: 60
memory_kb: 3584
final_rank: 8

code_snippet: #include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip> 
#include <stdio.h>
#include <assert.h>
#include <cstring>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end());
#define rep(i,n) for(int i=0;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define vec(...) vector<__VA_ARGS__>
#define _38d15hw ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
//eodefine
using namespace std;
using pii=pair<int,int>;
using vi=vec(int);
const int mxn=12000;
const int di[]={1,-1,0,0};
const int dj[]={0,0,1,-1};

int main(){
_38d15hw;
	int h,w,n;
	cin>>h>>w>>n;
	vec(vi) a(h,vi(w,-inf));
	rep(i,n){
		int x,y,val;
		cin>>x>>y>>val;
		x--,y--;
		a[x][y]=val;
	}

	auto f=[&](int x,int y,int z){
		bool pok=0;
		rep(i,h){
			rep(j,w){
				if(a[i][j]>(z+abs(x-i)+abs(y-j))) return pok=0;
			}
		}
		return pok=1;
	};

	rep(i,h){
		rep(j,w){
			if(a[i][j]!=-inf) continue;
			crep(z,-5000,5001){
				if(f(i,j,z)){
					a[i][j]=z;
					break;
				}
			}
		}
	}

	auto ok=[&](int x,int y){
		bool pok=1;
		if(min(x,y)<0) return pok=0;
		if(x>=h or y>=w) return pok=0;
		return pok=1;
	};

	bool gok=1;
	rep(i,h){
		rep(j,w){
			if(a[i][j]==-inf) gok=0;
			rep(dir,4){
				int nex=i+di[dir],ney=j+dj[dir];
				if(ok(nex,ney)){
					if(abs(a[nex][ney]-a[i][j])>1){
						// cout<<i<<" "<<j<<"\n";
						// cout<<nex<<" "<<ney<<"\n";
						gok=0;
					}
				}
			}
		}
	}
	// rep(i,h){ 
	// 	rep(j,w) cout<<a[i][j]<<" ";
	// 	cout<<"\n";
	// }
	int ans=0;
	rep(i,h){
		rep(j,w) ans+=a[i][j];
		// cout<<"\n";
	}
	if(!gok) cout<<"No\n";
	else cout<<ans<<"\n";
//
	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3468
final_rank: 7

code_snippet: #include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip> 
#include <stdio.h>
#include <assert.h>
#include <cstring>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end());
#define rep(i,n) for(int i=0;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define vec(...) vector<__VA_ARGS__>
#define _38d15hw ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
//eodefine
using namespace std;
using pii=pair<int,int>;
using vi=vec(int);
const int mxn=12000;
const int di[]={1,-1,0,0};
const int dj[]={0,0,1,-1};

int main(){
_38d15hw;
	int h,w,n;
	cin>>h>>w>>n;
	vec(vi) a(h,vi(w,-inf));
	rep(i,n){
		int x,y,val;
		cin>>x>>y>>val;
		x--,y--;
		a[x][y]=val;
	}

	auto f=[&](int x,int y,int z){
		bool pok=0;
		rep(i,h){
			rep(j,w){
				if(a[i][j]>(z+abs(x-i)+abs(y-j))) return pok=0;
			}
		}
		return pok=1;
	};

	rep(i,h){
		rep(j,w){
			if(a[i][j]!=-inf) continue;
			crep(z,-5000,5001){
				if(f(i,j,z)){
					a[i][j]=z;
					break;
				}
			}
		}
	}

	auto ok=[&](int x,int y){
		bool pok=1;
		if(min(x,y)<0) return pok=0;
		if(x>=h or y>=w) return pok=0;
		return pok=1;
	};

	bool gok=1;
	rep(i,h){
		rep(j,w){
			if(a[i][j]==-inf) gok=0;
			rep(dir,4){
				int nex=i+di[dir],ney=j+dj[dir];
				if(ok(nex,ney)){
					if(abs(a[nex][ney]-a[i][j])>1){
						cout<<i<<" "<<j<<"\n";
						cout<<nex<<" "<<ney<<"\n";
					}
				}
			}
		}
	}
	// rep(i,h){ 
	// 	rep(j,w) cout<<a[i][j]<<" ";
	// 	cout<<"\n";
	// }
	int ans=0;
	rep(i,h){
		rep(j,w) ans+=a[i][j];
		// cout<<"\n";
	}
	if(!gok) cout<<"No\n";
	else cout<<ans<<"\n";
//
	return 0;
}
accuracy: 0.037037037037037035
time_ms: 70
memory_kb: 3460
final_rank: 11

code_snippet: #include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip> 
#include <stdio.h>
#include <assert.h>
#include <cstring>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end());
#define rep(i,n) for(int i=0;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define vec(...) vector<__VA_ARGS__>
#define _38d15hw ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
//eodefine
using namespace std;
using pii=pair<int,int>;
using vi=vec(int);
const int mxn=12000;
const int di[]={1,-1,0,0};
const int dj[]={0,0,1,-1};

int main(){
_38d15hw;
	int h,w,n;
	cin>>h>>w>>n;
	vec(vi) a(h,vi(w,-inf));
	rep(i,n){
		int x,y,val;
		cin>>x>>y>>val;
		x--,y--;
		a[x][y]=val;
	}

	auto f=[&](int x,int y,int z){
		bool pok=0;
		rep(i,h){
			rep(j,w){
				if(a[i][j]>(z+abs(x-i)+abs(y-j))) return pok=0;
			}
		}
		return pok=1;
	};

	rep(i,h){
		rep(j,w){
			if(a[i][j]!=-inf) continue;
			crep(z,-5000,5001){
				if(f(i,j,z)){
					a[i][j]=z;
					break;
				}
			}
		}
	}

	auto ok=[&](int x,int y){
		bool pok=1;
		if(min(x,y)<0) return pok=0;
		if(x>=h or y>=w) return pok=0;
		return pok=1;
	};

	bool gok=1;
	rep(i,h){
		rep(j,w){
			if(a[i][j]==-inf) gok=0;
			rep(dir,4){
				int nex=i+di[i],ney=j+dj[i];
				if(ok(nex,ney)){
					if(abs(a[nex][ney]-a[i][j])>1) gok=0;
				}
			}
		}
	}
	int ans=0;
	rep(i,h){
		rep(j,w) ans+=a[i][j];
		// cout<<"\n";
	}
	if(!gok) cout<<"No\n";
	else cout<<ans<<"\n";
//
	return 0;
}
accuracy: 0.018518518518518517
time_ms: 60
memory_kb: 3372
final_rank: 14

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <array>
#include <tuple>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <cmath>
#include <functional>
#include <cassert>
#include <climits>
#include <iomanip>
#include <numeric>
#include <memory>
#include <random>
#define all(obj) (obj).begin(), (obj).end()
#define range(i, l, r) for(int i=l;i<r;i++)
#define bit_subset(i, S) for(int i=S, zero_cnt=0;(zero_cnt+=i==S)<2;i=(i-1)&S)
#define bit_kpop(i, n, k) for(int i=(1<<k)-1,x_bit,y_bit;i<(1<<n);x_bit=(i&-i),y_bit=i+x_bit,i=(!i?(1<<n):((i&~y_bit)/x_bit>>1)|y_bit))
#define bit_kth(i, k) ((i >> k)&1)
#define bit_highest(i) (i?63-__builtin_clzll(i):-1)
#define bit_lowest(i) (i?__builtin_ctzll(i):-1)
#define c2(n) ((ll(n)*(n-1))/2)
#define c3(n) (((ll(n)*(n-1))*(n-2))/6)
using ll = long long;
using ld = long double;
using ul = uint64_t;
using pi = std::pair<int, int>;
using pl = std::pair<ll, ll>;
using namespace std;

constexpr uint32_t MASK32 = 0xffffffff;
constexpr uint32_t SIGN32 = 0x80000000;
uint64_t encode_signed(int a, int b){return ((uint64_t(a)+SIGN32)<<32)+(uint64_t(b)+SIGN32);}
pi decode_signed(uint64_t x){return {int((x>>32)-SIGN32),int((x&MASK32)-SIGN32)};}
ll encode(int a, int b){return (ll(a)<<32)+b;}
pi decode(ll x){return {x>>32,x&MASK32};}

template<typename F, typename S>
std::ostream &operator<<(std::ostream &dest, const std::pair<F, S> &p){
  dest << p.first << ' ' << p.second;
  return dest;
}
template<typename T>
std::ostream &operator<<(std::ostream &dest, const std::vector<std::vector<T>> &v){
  int sz = v.size();
  if(sz==0) return dest;
  for(int i=0;i<sz;i++){
    int m = v[i].size();
    for(int j=0;j<m;j++) dest << v[i][j] << (i!=sz-1&&j==m-1?'\n':' ');
  }
  return dest;
}
template<typename T>
std::ostream &operator<<(std::ostream &dest, const std::vector<T> &v){
  int sz = v.size();
  if(sz==0) return dest;
  for(int i=0;i<sz-1;i++) dest << v[i] << ' ';
  dest << v[sz-1];
  return dest;
}
template<typename T, size_t sz>
std::ostream &operator<<(std::ostream &dest, const std::array<T, sz> &v){
  if(sz==0) return dest;
  for(int i=0;i<sz-1;i++) dest << v[i] << ' ';
  dest << v[sz-1];
  return dest;
}
template<typename T>
std::ostream &operator<<(std::ostream &dest, const std::set<T> &v){
  for(auto itr=v.begin();itr!=v.end();){
    dest << *itr;
    itr++;
    if(itr!=v.end()) dest << ' ';
  }
  return dest;
}
template<typename T, typename E>
std::ostream &operator<<(std::ostream &dest, const std::map<T, E> &v){
  for(auto itr=v.begin();itr!=v.end();){
    dest << '(' << itr->first << ", " << itr->second << ')';
    itr++;
    if(itr!=v.end()) dest << '\n';
  }
  return dest;
}
template<typename T>
vector<T> make_vec(size_t sz, T val){return std::vector<T>(sz, val);}
template<typename T, typename... Tail>
auto make_vec(size_t sz, Tail ...tail){
  return std::vector<decltype(make_vec<T>(tail...))>(sz, make_vec<T>(tail...));
}
template<typename T>
vector<T> read_vec(size_t sz){
  std::vector<T> v(sz);
  for(int i=0;i<sz;i++) std::cin >> v[i];
  return v;
}
template<typename T, typename... Tail>
auto read_vec(size_t sz, Tail ...tail){
  auto v = std::vector<decltype(read_vec<T>(tail...))>(sz);
  for(int i=0;i<sz;i++) v[i] = read_vec<T>(tail...);
  return v;
}
template<typename T>
bool mul_overflow(T a, T b){
  static T INF = numeric_limits<T>::max();
  return (INF / a) < b;
}
template<typename T>
bool add_overflow(T a, T b){
  static T INF = numeric_limits<T>::max();
  return (INF - a) < b;
}
template<typename T>
bool chmax(T &a, const T b){
  if(a >= b) return false;
  a = b;
  return true;
}
template<typename T>
bool chmin(T &a, const T b){
  if(a <= b) return false;
  a = b;
  return true;
}
template<typename T>
T max(vector<T> &v, int l=0, int r = -1){
  return *std::max_element(v.begin()+l, v.begin()+(r==-1?v.size():r));
}
template<typename T>
T min(vector<T> &v, int l=0, int r = -1){
  return *std::min_element(v.begin()+l, v.begin()+(r==-1?v.size():r));
}
template<typename T>
int argmax(vector<T> &v, int l=0, int r=-1){
  T res = max(v, l, r);
  if(r==-1) r = v.size();
  for(int i=l;i<r;i++) if(v[i]==res) return i;
  return -1;
}
template<typename T>
int argmin(vector<T> &v, int l=0, int r=-1){
  T res = min(v, l, r);
  if(r==-1) r = v.size();
  for(int i=l;i<r;i++) if(v[i]==res) return i;
  return -1;
}
ll gcd(ll _a, ll _b) {
  uint64_t a = abs(_a), b = abs(_b);
  if(a == 0) return b;
  if(b == 0) return a;
  int shift = __builtin_ctzll(a | b);
  a >>= __builtin_ctzll(a);
  do{
    b >>= __builtin_ctzll(b);
    if(a > b) std::swap(a, b);
    b -= a;
  }while(b);
  return (a << shift);
}
ll lcm(ll a, ll b){
  return (a / gcd(a, b)) * b;
}
ll llpow(ll a, ll b){
  ll ret = 1, mul = a;
  while(b){
    if(b&1) ret *= mul;
    mul *= mul;
    b >>= 1;
  }
  return ret;
}
template<int MOD>
ll mpow(ll a, ll b){
  int ret = (MOD==1?0:1), mul = a % MOD;
  while(b){
    if(b&1) ret = (ll(ret) * mul) % MOD;
    mul = (ll(mul) * mul) % MOD;
    b >>= 1;
  }
  return ret;
}
ll mpow(ll a, ll b, ll MOD){
  int ret = (MOD==1?0:1), mul = a % MOD;
  while(b){
    if(b&1) ret = (ll(ret) * mul) % MOD;
    mul = (ll(mul) * mul) % MOD;
    b >>= 1;
  }
  return ret;
}

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w, n;std::cin >> h >> w >> n;
  auto mm = make_vec<pi>(h, w, pi{-1000000, 1000000});
  auto is_p = make_vec<bool>(h, w, false);

  range(i, 0, n){
    int x, y, z;std::cin >> x >> y >> z;
    x--, y--;
    mm[x][y] = pi{z, z};
    is_p[x][y] = true;
  }
  int dx[4] = {0, 0, 1, -1}, dy[4] = {-1, 1, 0, 0};

  for(int step = 0;step <= 10000; step++){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(is_p[i][j]) continue;
        for(int k=0;k<4;k++){
          int x = i + dx[k], y = j + dy[k];
          if(x<0||x>=h||y<0||y>=w) continue;
          mm[i][j].first = max(mm[i][j].first, mm[x][y].first-1);
          mm[i][j].second = min(mm[i][j].second, mm[x][y].second+1);
        }
      }
    }
  }

  ll ans = 0;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(mm[i][j].first>mm[i][j].second){
        std::cout << "No" << '\n';
        return 0;
      }
      //std::cout << mm[i][j] << '\n';
      ans += mm[i][j].first;
    }
  }
  std::cout << ans << '\n';
}
accuracy: 1
time_ms: 260
memory_kb: 3488
final_rank: 9

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <array>
#include <tuple>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <cmath>
#include <functional>
#include <cassert>
#include <climits>
#include <iomanip>
#include <numeric>
#include <memory>
#include <random>
#define all(obj) (obj).begin(), (obj).end()
#define range(i, l, r) for(int i=l;i<r;i++)
#define bit_subset(i, S) for(int i=S, zero_cnt=0;(zero_cnt+=i==S)<2;i=(i-1)&S)
#define bit_kpop(i, n, k) for(int i=(1<<k)-1,x_bit,y_bit;i<(1<<n);x_bit=(i&-i),y_bit=i+x_bit,i=(!i?(1<<n):((i&~y_bit)/x_bit>>1)|y_bit))
#define bit_kth(i, k) ((i >> k)&1)
#define bit_highest(i) (i?63-__builtin_clzll(i):-1)
#define bit_lowest(i) (i?__builtin_ctzll(i):-1)
#define c2(n) ((ll(n)*(n-1))/2)
#define c3(n) (((ll(n)*(n-1))*(n-2))/6)
using ll = long long;
using ld = long double;
using ul = uint64_t;
using pi = std::pair<int, int>;
using pl = std::pair<ll, ll>;
using namespace std;

constexpr uint32_t MASK32 = 0xffffffff;
constexpr uint32_t SIGN32 = 0x80000000;
uint64_t encode_signed(int a, int b){return ((uint64_t(a)+SIGN32)<<32)+(uint64_t(b)+SIGN32);}
pi decode_signed(uint64_t x){return {int((x>>32)-SIGN32),int((x&MASK32)-SIGN32)};}
ll encode(int a, int b){return (ll(a)<<32)+b;}
pi decode(ll x){return {x>>32,x&MASK32};}

template<typename F, typename S>
std::ostream &operator<<(std::ostream &dest, const std::pair<F, S> &p){
  dest << p.first << ' ' << p.second;
  return dest;
}
template<typename T>
std::ostream &operator<<(std::ostream &dest, const std::vector<std::vector<T>> &v){
  int sz = v.size();
  if(sz==0) return dest;
  for(int i=0;i<sz;i++){
    int m = v[i].size();
    for(int j=0;j<m;j++) dest << v[i][j] << (i!=sz-1&&j==m-1?'\n':' ');
  }
  return dest;
}
template<typename T>
std::ostream &operator<<(std::ostream &dest, const std::vector<T> &v){
  int sz = v.size();
  if(sz==0) return dest;
  for(int i=0;i<sz-1;i++) dest << v[i] << ' ';
  dest << v[sz-1];
  return dest;
}
template<typename T, size_t sz>
std::ostream &operator<<(std::ostream &dest, const std::array<T, sz> &v){
  if(sz==0) return dest;
  for(int i=0;i<sz-1;i++) dest << v[i] << ' ';
  dest << v[sz-1];
  return dest;
}
template<typename T>
std::ostream &operator<<(std::ostream &dest, const std::set<T> &v){
  for(auto itr=v.begin();itr!=v.end();){
    dest << *itr;
    itr++;
    if(itr!=v.end()) dest << ' ';
  }
  return dest;
}
template<typename T, typename E>
std::ostream &operator<<(std::ostream &dest, const std::map<T, E> &v){
  for(auto itr=v.begin();itr!=v.end();){
    dest << '(' << itr->first << ", " << itr->second << ')';
    itr++;
    if(itr!=v.end()) dest << '\n';
  }
  return dest;
}
template<typename T>
vector<T> make_vec(size_t sz, T val){return std::vector<T>(sz, val);}
template<typename T, typename... Tail>
auto make_vec(size_t sz, Tail ...tail){
  return std::vector<decltype(make_vec<T>(tail...))>(sz, make_vec<T>(tail...));
}
template<typename T>
vector<T> read_vec(size_t sz){
  std::vector<T> v(sz);
  for(int i=0;i<sz;i++) std::cin >> v[i];
  return v;
}
template<typename T, typename... Tail>
auto read_vec(size_t sz, Tail ...tail){
  auto v = std::vector<decltype(read_vec<T>(tail...))>(sz);
  for(int i=0;i<sz;i++) v[i] = read_vec<T>(tail...);
  return v;
}
template<typename T>
bool mul_overflow(T a, T b){
  static T INF = numeric_limits<T>::max();
  return (INF / a) < b;
}
template<typename T>
bool add_overflow(T a, T b){
  static T INF = numeric_limits<T>::max();
  return (INF - a) < b;
}
template<typename T>
bool chmax(T &a, const T b){
  if(a >= b) return false;
  a = b;
  return true;
}
template<typename T>
bool chmin(T &a, const T b){
  if(a <= b) return false;
  a = b;
  return true;
}
template<typename T>
T max(vector<T> &v, int l=0, int r = -1){
  return *std::max_element(v.begin()+l, v.begin()+(r==-1?v.size():r));
}
template<typename T>
T min(vector<T> &v, int l=0, int r = -1){
  return *std::min_element(v.begin()+l, v.begin()+(r==-1?v.size():r));
}
template<typename T>
int argmax(vector<T> &v, int l=0, int r=-1){
  T res = max(v, l, r);
  if(r==-1) r = v.size();
  for(int i=l;i<r;i++) if(v[i]==res) return i;
  return -1;
}
template<typename T>
int argmin(vector<T> &v, int l=0, int r=-1){
  T res = min(v, l, r);
  if(r==-1) r = v.size();
  for(int i=l;i<r;i++) if(v[i]==res) return i;
  return -1;
}
ll gcd(ll _a, ll _b) {
  uint64_t a = abs(_a), b = abs(_b);
  if(a == 0) return b;
  if(b == 0) return a;
  int shift = __builtin_ctzll(a | b);
  a >>= __builtin_ctzll(a);
  do{
    b >>= __builtin_ctzll(b);
    if(a > b) std::swap(a, b);
    b -= a;
  }while(b);
  return (a << shift);
}
ll lcm(ll a, ll b){
  return (a / gcd(a, b)) * b;
}
ll llpow(ll a, ll b){
  ll ret = 1, mul = a;
  while(b){
    if(b&1) ret *= mul;
    mul *= mul;
    b >>= 1;
  }
  return ret;
}
template<int MOD>
ll mpow(ll a, ll b){
  int ret = (MOD==1?0:1), mul = a % MOD;
  while(b){
    if(b&1) ret = (ll(ret) * mul) % MOD;
    mul = (ll(mul) * mul) % MOD;
    b >>= 1;
  }
  return ret;
}
ll mpow(ll a, ll b, ll MOD){
  int ret = (MOD==1?0:1), mul = a % MOD;
  while(b){
    if(b&1) ret = (ll(ret) * mul) % MOD;
    mul = (ll(mul) * mul) % MOD;
    b >>= 1;
  }
  return ret;
}

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w, n;std::cin >> h >> w >> n;
  auto mm = make_vec<pi>(h, w, pi{-1000000, 1000000});
  auto is_p = make_vec<bool>(h, w, false);

  range(i, 0, n){
    int x, y, z;std::cin >> x >> y >> z;
    x--, y--;
    mm[x][y] = pi{z, z};
    is_p[x][y] = true;
  }
  int dx[4] = {0, 0, 1, -1}, dy[4] = {-1, 1, 0, 0};

  for(int step = 0;step <= 10000; step++){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        if(is_p[i][j]) continue;
        for(int k=0;k<4;k++){
          int x = i + dx[k];
          int y = j + dy[k];
          if(x<0||x>=h||y<0||y>=w) continue;
          mm[i][j].first = max(mm[i][j].first, mm[x][y].first-1);
          mm[i][j].second = min(mm[i][j].second, mm[x][y].second+1);
        }
      }
    }
  }

  ll ans = 0;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(mm[i][j].first>mm[i][j].second){
        std::cout << "NO" << '\n';
        return 0;
      }
      //std::cout << mm[i][j] << '\n';
      ans += mm[i][j].first;
    }
  }
  std::cout << ans << '\n';
}
accuracy: 0.037037037037037035
time_ms: 260
memory_kb: 3392
final_rank: 13

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <array>
#include <tuple>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <cmath>
#include <functional>
#include <cassert>
#include <climits>
#include <iomanip>
#include <numeric>
#include <memory>
#include <random>
#define all(obj) (obj).begin(), (obj).end()
#define range(i, l, r) for(int i=l;i<r;i++)
#define bit_subset(i, S) for(int i=S, zero_cnt=0;(zero_cnt+=i==S)<2;i=(i-1)&S)
#define bit_kpop(i, n, k) for(int i=(1<<k)-1,x_bit,y_bit;i<(1<<n);x_bit=(i&-i),y_bit=i+x_bit,i=(!i?(1<<n):((i&~y_bit)/x_bit>>1)|y_bit))
#define bit_kth(i, k) ((i >> k)&1)
#define bit_highest(i) (i?63-__builtin_clzll(i):-1)
#define bit_lowest(i) (i?__builtin_ctzll(i):-1)
#define c2(n) ((ll(n)*(n-1))/2)
#define c3(n) (((ll(n)*(n-1))*(n-2))/6)
using ll = long long;
using ld = long double;
using ul = uint64_t;
using pi = std::pair<int, int>;
using pl = std::pair<ll, ll>;
using namespace std;

constexpr uint32_t MASK32 = 0xffffffff;
constexpr uint32_t SIGN32 = 0x80000000;
uint64_t encode_signed(int a, int b){return ((uint64_t(a)+SIGN32)<<32)+(uint64_t(b)+SIGN32);}
pi decode_signed(uint64_t x){return {int((x>>32)-SIGN32),int((x&MASK32)-SIGN32)};}
ll encode(int a, int b){return (ll(a)<<32)+b;}
pi decode(ll x){return {x>>32,x&MASK32};}

template<typename F, typename S>
std::ostream &operator<<(std::ostream &dest, const std::pair<F, S> &p){
  dest << p.first << ' ' << p.second;
  return dest;
}
template<typename T>
std::ostream &operator<<(std::ostream &dest, const std::vector<std::vector<T>> &v){
  int sz = v.size();
  if(sz==0) return dest;
  for(int i=0;i<sz;i++){
    int m = v[i].size();
    for(int j=0;j<m;j++) dest << v[i][j] << (i!=sz-1&&j==m-1?'\n':' ');
  }
  return dest;
}
template<typename T>
std::ostream &operator<<(std::ostream &dest, const std::vector<T> &v){
  int sz = v.size();
  if(sz==0) return dest;
  for(int i=0;i<sz-1;i++) dest << v[i] << ' ';
  dest << v[sz-1];
  return dest;
}
template<typename T, size_t sz>
std::ostream &operator<<(std::ostream &dest, const std::array<T, sz> &v){
  if(sz==0) return dest;
  for(int i=0;i<sz-1;i++) dest << v[i] << ' ';
  dest << v[sz-1];
  return dest;
}
template<typename T>
std::ostream &operator<<(std::ostream &dest, const std::set<T> &v){
  for(auto itr=v.begin();itr!=v.end();){
    dest << *itr;
    itr++;
    if(itr!=v.end()) dest << ' ';
  }
  return dest;
}
template<typename T, typename E>
std::ostream &operator<<(std::ostream &dest, const std::map<T, E> &v){
  for(auto itr=v.begin();itr!=v.end();){
    dest << '(' << itr->first << ", " << itr->second << ')';
    itr++;
    if(itr!=v.end()) dest << '\n';
  }
  return dest;
}
template<typename T>
vector<T> make_vec(size_t sz, T val){return std::vector<T>(sz, val);}
template<typename T, typename... Tail>
auto make_vec(size_t sz, Tail ...tail){
  return std::vector<decltype(make_vec<T>(tail...))>(sz, make_vec<T>(tail...));
}
template<typename T>
vector<T> read_vec(size_t sz){
  std::vector<T> v(sz);
  for(int i=0;i<sz;i++) std::cin >> v[i];
  return v;
}
template<typename T, typename... Tail>
auto read_vec(size_t sz, Tail ...tail){
  auto v = std::vector<decltype(read_vec<T>(tail...))>(sz);
  for(int i=0;i<sz;i++) v[i] = read_vec<T>(tail...);
  return v;
}
template<typename T>
bool mul_overflow(T a, T b){
  static T INF = numeric_limits<T>::max();
  return (INF / a) < b;
}
template<typename T>
bool add_overflow(T a, T b){
  static T INF = numeric_limits<T>::max();
  return (INF - a) < b;
}
template<typename T>
bool chmax(T &a, const T b){
  if(a >= b) return false;
  a = b;
  return true;
}
template<typename T>
bool chmin(T &a, const T b){
  if(a <= b) return false;
  a = b;
  return true;
}
template<typename T>
T max(vector<T> &v, int l=0, int r = -1){
  return *std::max_element(v.begin()+l, v.begin()+(r==-1?v.size():r));
}
template<typename T>
T min(vector<T> &v, int l=0, int r = -1){
  return *std::min_element(v.begin()+l, v.begin()+(r==-1?v.size():r));
}
template<typename T>
int argmax(vector<T> &v, int l=0, int r=-1){
  T res = max(v, l, r);
  if(r==-1) r = v.size();
  for(int i=l;i<r;i++) if(v[i]==res) return i;
  return -1;
}
template<typename T>
int argmin(vector<T> &v, int l=0, int r=-1){
  T res = min(v, l, r);
  if(r==-1) r = v.size();
  for(int i=l;i<r;i++) if(v[i]==res) return i;
  return -1;
}
ll gcd(ll _a, ll _b) {
  uint64_t a = abs(_a), b = abs(_b);
  if(a == 0) return b;
  if(b == 0) return a;
  int shift = __builtin_ctzll(a | b);
  a >>= __builtin_ctzll(a);
  do{
    b >>= __builtin_ctzll(b);
    if(a > b) std::swap(a, b);
    b -= a;
  }while(b);
  return (a << shift);
}
ll lcm(ll a, ll b){
  return (a / gcd(a, b)) * b;
}
ll llpow(ll a, ll b){
  ll ret = 1, mul = a;
  while(b){
    if(b&1) ret *= mul;
    mul *= mul;
    b >>= 1;
  }
  return ret;
}
template<int MOD>
ll mpow(ll a, ll b){
  int ret = (MOD==1?0:1), mul = a % MOD;
  while(b){
    if(b&1) ret = (ll(ret) * mul) % MOD;
    mul = (ll(mul) * mul) % MOD;
    b >>= 1;
  }
  return ret;
}
ll mpow(ll a, ll b, ll MOD){
  int ret = (MOD==1?0:1), mul = a % MOD;
  while(b){
    if(b&1) ret = (ll(ret) * mul) % MOD;
    mul = (ll(mul) * mul) % MOD;
    b >>= 1;
  }
  return ret;
}

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w, n;std::cin >> h >> w >> n;
  auto mm = make_vec<pi>(h, w, pi{-1000000, 1000000});

  range(i, 0, n){
    int x, y, z;std::cin >> x >> y >> z;
    x--, y--;
    mm[x][y] = pi{z, z};
  }
  int dx[4] = {0, 0, 1, -1};
  int dy[4] = {-1, 1, 0, 0};
  for(int step = 0;step <= 10000; step++){
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
        for(int k=0;k<4;k++){
          int x = i + dx[k];
          int y = j + dy[k];
          if(x<0||x>=h||y<0||y>=w) continue;
          mm[i][j].first = max(mm[i][j].first, mm[x][y].first-1);
          mm[i][j].second = min(mm[i][j].second, mm[x][y].second+1);
        }
      }
    }
  }
  ll ans = 0;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(mm[i][j].first>mm[i][j].second){
        std::cout << "NO" << '\n';
        return 0;
      }
      //std::cout << mm[i][j] << '\n';
      ans += mm[i][j].first;
    }
  }
  std::cout << ans << '\n';
}
accuracy: 0.037037037037037035
time_ms: 200
memory_kb: 3484
final_rank: 12

code_snippet: /* Aa^~ kokoro ga pyonpyon suru n jaa^~
// ZZZXXkXkkkZ!``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ```?Wfpppbpbbpbbpbbbkbkk
// ppbbbpbbpVr`` `` ` ` ` ` ```` `` ` ` ``  ` ` ` ` ` ` ` ` `  dppbbkkkkkkkkkkqkqkk
// HkqqqqqkkWr`` ` ` ``` ``` `?G, ` ` ``.JC!```` ` ` `` `` ````(Wpbkkkkkkkkqkkkkkqk
// mmmmmqqqqpr` `` `` ```````.+zT=`` `` 7TO-.```````` `` `` ```(yppbkkkkkkkkkkkkkkk
// ggmgmmqqqH$ ``````````....````` `  ````````.`````` `` ``````.yfpppbbbbkkkkqqqqqH
// gmmmmmqqqkW<```` `````...`````  .,.`  ````....````` ``````` (Wbqqmgmmgmggggggggg
// qmmmqqqqkkWk.``````````````````` ;:<`` `````.`````````````-_<-?WHHqmmmmmmgmmgggg
// @@@@@@@gggHH6- ``````````````` `` _ `` ```````````````` ._~~_.`-?Wkqmmmmmmmggg@g
// @@@@g@gggHY~.-<_- `````````````````````````````````` ._~~(<-``.`.(WHqqqmmggggmmm
// @@g@gggHH=.`..._<-___..```````````````````````. .-_~~~_(!``-.``.`` OHHWUWHmqHWXW
// gggggmqK1.``..~.. _<<+-(____..  ````````  ..__~~_((<<!.`.``` .``.`` j0C1XUHmHIdW
// ggmmqH0!,_``.>`````` _<<;<v<<<++((((((((((<<<<<<~_. (-.``~``.>..```  jOuWHHqHIdH
// gmmqkW!`(_ J>`    `` ` _~<`_~~~~<<<<<<<~~__````````` ?1. ._`(__```    zXWHg@HkXH
// gHHWS{``(lJ<!``.``.```(:+>`._`````.` <..`` - ``. `   _ ?&._.I`_`````` .XyVfppppW
// HHHSv``.(X:_..... _..(;+<!.(<..-.....-.-_..+_`..<.`.`..`_IJd}  .`..````jqg@@@@@@
// XHWZ{..<Jk~!.`.. (<.-(+>(_.(1.(_..`.`.`.<_.+<_..<<-..._..-zy>.`_`...```.WH@HHHHH
// bkWt~.-jCz(_..`.(+<.(;< ._-<=_(<_..-....(_.<1<..(<<.`._..-JUS-._.`...```dHmH9VUH
// WUUO..(f.(c...__+z<-(+~` _-+<_(><..__.`.(<._.z_.(1;_..__.(C(zT-(..`...``(WHR<+Xk
// kkkk._(_.->..._(z;:_><.._>_+_<(1>_._<...(v<<.(<.(+z<..-_(Z~_<_j+_..`...`(WHKz1ZW
// @@gR._+_..~..-<+z<<?<>```_.<_.(+1><_;_..(1_:`.<<??1z--(+Z!..<_.j<....`..(bgHAAQX
// @@mR.(j:..~.._<z!`.(>~``` ~(_.(+<1><><_.(((_`.<__`.<_.(X>...<_.(<.....`.JUWWWyWW
// @gmH_(zl..(.._+>```<+_````.~>``(+.<?>>_._(<```(<<``(__<>....<.._<.......dXkkkHHH
// mmqHl(dk_.(_.-=~`.`.1-..._~-1.``_:`(??<_~(`.--.&_.`.<(;<...._.._<..`..._Xg@@@@@@
// qHkpk(dX<.(;..j_```.(((JJ&a&-~``````.1<_```-(((e+.-(/`(>...._..(<......(Wmggg@@g
// HVHbWcz><__+_.(_.(dWWHHH@HHc~````````.+~`` (jHMMMHHHm&.?..._<..(<_..._.(WqqHHmHg
// 0>vWWkzZwl~<o.__`__~X@@HM@Hb ```.`.``. ```` d@@HHH@@K?76...(<..(<_...(_(ppWWWWHq
// X0XWHKXXw$<(z<.( `` WHHMHHHH_``````````````.WHHMNMHHH_`(...(<_.(z_..._<(fWVC174W
// XuXWHHWWz>__+z+.!`..??CZYCOX_`````````````.`~.OvTUZUS_`~.._+?_.(_~_.._zjO=1+~+jy
// kkkkkkkkX:._<z=1(_`` << ``->``.``.``.``.```` ?<`` (v!`._..(??_.(1._.._=dUOOzzzwX
// @@@@@@@@H<...1O=v<_...__ -_````````````````.`` `` ~.`` :.~+=?~.(;_(...jdQQQQQkkk
// H@@@@@@@H~...(==>.~~~~~....`.`````````.`````.`........->.(===~~<<.(...(dg@@@@@@@
// @@@H@@HHH_.__(=l>~.~~~~~....``.``.``.```..`......~~~~~(<_+=l=~_<.->..~_dqggggg@g
// @H@@@@MHH_._<(=l>...........```````````````.`...~~~~~~+<(=lz=~((j=z_..~jWqmmgggm
// @@H@@HHWH_._<(lll-.......```.````.``.`..`````........_z<+llZz~(lOO=<...(VYUUUW9Y
// @@HMMHWZf>~_=:=llw+.`````````.`.```__~~_``.`````.....(z+llOOz_zllOlz~..~<<1+dW>_
// MMM#MHHWXl~_=>1ltwOl&.`.``.`````.``````````````.````.(llttwtz(OltwOz<..__zwOwwOz
// HM#HMHUUI<._1z+ttOZttlt&....``.``.`.````.``...``...(zZtttOktzjttttwlz_._<(Xkkkkk
// HHHmHSZu:(_~+OztttXtttOZZttO+-..............-(+ztOttwttttd0tOZttttwOl<~.(_dMMHHH
// rvuuXuuI~~<~(uttttwvOwwwkQQHMMHHHHHHHHHMMMNmgey?OwwwrtttwXOtwttttttXtO-~.((wZyyy
// HHHHHHK>(~(-(dOrtrrl(QgMHMMMHHHHHHHHHHHHHHHH##HMNkX0rrrrXXrd%`` (Ctwwtz_~.<(Wg@H
// NNNNNHD(~(zo~zXrrrQdHHMMNMHHHHHHHHHHHHHHHHHHHHHH##HNmyrdKkwZ ` _``-zwrt1~~_<(MNM
// MMMMM#<<_jwr:(Z4QHHMMHMHHHHHHHHHHHHHHHHHHHHHHHHHHHH###NHSXZ>` ~````.OXtt>~._<?MM
*/

#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second

template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class T>
istream& operator>>(istream& is, vector<T>& xs){
  for(auto& x: xs)
	is >> x;
  return is;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T>& xs){
  for(unsigned int i=0;i<xs.size();++i)
	os << (i?" ":"") << xs[i];
  return os;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}
void debug(istringstream&){}
template <char sep=',', class Head, class... Tail>
void debug(istringstream& iss, Head&& head, Tail&&... tail)
{
  string name;
  getline(iss, name, ',');
  cout << sep << name << "=" << head;
  debug(iss, forward<Tail>(tail)...);
}

#ifdef PYONPOI
#define DEBUG(...) \
  do{ \
    istringstream ss(#__VA_ARGS__); \
    debug<' '>(ss, __VA_ARGS__); \
    cout<<endl; \
  }while(0)
#else
#define DEBUG
#endif
void init_io(int argc, char* argv[]){
  cin.tie(0);
  ios_base::sync_with_stdio(false);
#ifdef PYONPOI
  if(argc > 1) {
    ifstream *ifs = new ifstream();
    ifs->open(argv[1]);
    cin.rdbuf(ifs->rdbuf());
  }
#endif
}


const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;

int main(int argc, char* argv[]){
  init_io(argc, argv);

  int W, H, N;
  cin >> W >> H >> N;
  vector<pair<int,PII>> xs(N);
  for(auto& x: xs) {
    cin >> x.SS.FF >> x.SS.SS >> x.FF;
    x.SS.FF--;
    x.SS.SS--; 
  }
  auto isin = [&](PII p){ return 0 <= p.FF && p.FF < W && 0 <= p.SS && p.SS < H; };

  RSORT(xs);

  VVI low(H, VI(W));
  {
    auto tmp = xs[0];
    int cx = tmp.SS.FF;
    int cy = tmp.SS.SS;
    low[cy][cx] = tmp.FF;
    int ub = W + H;
    FOR(d,1,ub+1){
      int base = low[cy][cx] - d;
      REP(i,d){
        vector<PII> ps;
        ps.EB(cx-d+i, cy-i);
        ps.EB(cx+i, cy-d+i);
        ps.EB(cx+d-i, cy+i);
        ps.EB(cx-i, cy+d-i);
        for(auto p : ps){
          if(!isin(p)) continue;
          low[p.SS][p.FF] = base;
        }
      }
    }
  }

  bool ok = true;
  FOR(i,1,SZ(xs)){
    auto tmp = xs[i];
    int cx = tmp.SS.FF;
    int cy = tmp.SS.SS;
    if(low[cy][cx] > tmp.FF){
      ok = false;
      break;
    }
    int del = tmp.FF - low[cy][cx];
    low[cy][cx] += del;

    FOR(d,1,H+W+1){
      int base = low[cy][cx] - d;
      REP(i,d){
        vector<PII> ps;
        ps.EB(cx-d+i, cy-i);
        ps.EB(cx+i, cy-d+i);
        ps.EB(cx+d-i, cy+i);
        ps.EB(cx-i, cy+d-i);
        for(auto p : ps){
          if(!isin(p)) continue;
          maxi(low[p.SS][p.FF], base);
        }
      }
    }
  }

  REP(y,H) REP(x,W){
    if(x+1 < W && abs(low[y][x] - low[y][x+1]) > 1) ok = false;
    if(y+1 < H && abs(low[y][x] - low[y+1][x]) > 1) ok = false;
  }

  if(!ok){
    cout << "No" << endl;
  }
  else{
    int ans = 0;
    REP(y,H) REP(x,W) ans += low[y][x];
    cout << ans << endl;
  }

  REP(y,H) DEBUG(low[y]);

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3216
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;

int W, D, N;
bool iss[55][55];
int Min[55][55], Max[55][55];

void adjust(int x, int y)
{
    for(int i = 1; i <= W; i++)
        for(int j = 1; j <= D; j++)
    {
        int dis = abs(i-x)+abs(j-y);
        Min[i][j] = max( Min[i][j], Min[x][y]-dis );
        Max[i][j] = min( Max[i][j], Max[x][y]+dis );
    }
    //cout<<x<<' '<<y<<' '<<Min[x][y]<<' '<<Max[x][y]<<' '<<Min[1][1]<<' '<<Max[1][1]<<'\n';
}

int main()
{
    cin>>W>>D>>N;
    int x, y, z;
    for(int i = 1; i <= N; i++)
    {
        cin>>x>>y>>z;
        iss[x][y] = 1;
        Min[x][y] = Max[x][y] = z;
    }
    for(int i = 1; i <= W; i++)
        for(int j = 1; j <= D; j++)
            if(!iss[i][j])Min[i][j]=-200, Max[i][j]=200;

    for(int i = 1; i <= W; i++)
        for(int j = 1; j <= D; j++)
            adjust(i,j);

    for(int i = 1; i <= W; i++)
        for(int j = 1; j <= D; j++)
            if( Min[i][j] > Max[i][j] )
    {
        //cout<<i<<' '<<j<<' '<<Min[i][j]<<' '<<Max[i][j]<<'\n';
        cout<<"No\n";
        return 0;
    }

    int ans = 0;
    for(int i = 1; i <= W; i++)for(int j = 1; j <= D; j++)ans+=Min[i][j];

    cout<<ans<<'\n';

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3128
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

int W, D, N;
bool iss[55][55];
int Min[55][55], Max[55][55];

void adjust(int x, int y)
{
    for(int i = 1; i <= W; i++)
        for(int j = 1; j <= D; j++)
    {
        int dis = abs(i-x)+abs(j-y);
        Min[i][j] = max( Min[i][j], Min[x][y]-dis );
        Max[i][j] = min( Max[i][j], Max[x][y]+dis );
    }
    //cout<<x<<' '<<y<<' '<<Min[x][y]<<' '<<Max[x][y]<<' '<<Min[1][1]<<' '<<Max[1][1]<<'\n';
}

int main()
{
    cin>>W>>D>>N;
    int x, y, z;
    for(int i = 1; i <= N; i++)
    {
        cin>>x>>y>>z;
        iss[x][y] = 1;
        Min[x][y] = Max[x][y] = z;
    }
    for(int i = 1; i <= W; i++)
        for(int j = 1; j <= D; j++)
            if(!iss[i][j])Min[i][j]=-200, Max[i][j]=200;

    for(int i = 1; i <= W; i++)
        for(int j = 1; j <= D; j++)
            adjust(i,j);

    for(int i = 1; i <= W; i++)
        for(int j = 1; j <= D; j++)
            if( Min[i][j] > Max[i][j] )
    {
        //cout<<i<<' '<<j<<' '<<Min[i][j]<<' '<<Max[i][j]<<'\n';
        cout<<"NO\n";
        return 0;
    }

    int ans = 0;
    for(int i = 1; i <= W; i++)for(int j = 1; j <= D; j++)ans+=Min[i][j];

    cout<<ans<<'\n';

    return 0;
}
accuracy: 0.037037037037037035
time_ms: 10
memory_kb: 3120
final_rank: 10

code_snippet: #include<bits/stdc++.h>
#define ll long long
#define rep(i, n) for(int i=0;i<(n);++i)
#define per(i, n) for(int i=(n)-1;i>=0;--i)
#define repa(i, n) for(int i=1;i<(n);++i)
#define lp(i, n) rep(i, n)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll<(x))
#define MOD ((ll)1e9+7)
#define INF MOD
using namespace std;
int a[100][100];
int m[100][100];
int w,d,n;

bool calc(int x,int y,int now){
  int nx[4]={1,-1,0,0},ny[4]={0,0,1,-1};
  lp(i,4){
    int nex=x+nx[i];
    int ney=y+ny[i];
    if(nex<0||nex==w)continue;
    if(ney<0||ney==d)continue;
    if(m[nex][ney]==INF){
      m[nex][ney]=now-1;
      calc(nex,ney,now-1);
      continue;
    }
    if(m[nex][ney]==now-1){
      continue;
    }
    if(m[nex][ney]<now){
      m[nex][ney]=now-1;
      calc(nex,ney,now-1);
      continue;
    }
  }
}

signed main(){
  cin>>w>>d>>n;
  lp(i,100){
    lp(j,100){
      m[i][j]=INF;
    }
  }
  vector<pair<int,pair<int,int>>> v;
  lp(i,n){
    int x,y,z;
    cin>>x>>y>>z;
    x--;
    y--;
    v.push_back({z,{x,y}});
    m[x][y]=z;
  }
  sort(all(v));
  reverse(all(v));
  lp(i,n){
    lp(j,n){
      if(i==j)continue;
      int dis=abs(v[i].second.first-v[j].second.first);
      dis+=abs(v[i].second.second-v[j].second.second);
      int des=abs(v[i].first-v[j].first);
      if(des>dis){
        cout<<"No"<<endl;
        return 0;
      }
    }
  }
  lp(i,n){
    m[v[i].second.first][v[i].second.second]=v[i].first;
    calc(v[i].second.first,v[i].second.second,v[i].first);
  }
  int ans=0;
  lp(i,w){
    lp(j,d){
      ans+=m[i][j];
      //cout<<m[i][j];
    }
    //cout<<endl;
  }
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3388
final_rank: 4

code_snippet: //#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
//#undef LOCAL
#include <bits/stdc++.h>
using namespace std;

#pragma region template

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#ifdef LOCAL
struct PrettyOS {
    ostream& os;
    bool first;
    template <class T> auto operator<<(T&& x) {
        if (!first) os << ", ";
        first = false;
        os << x;
        return *this;
    }
};
template <class... T> void dbg0(T&&... t) {
    (PrettyOS{cerr, true} << ... << t);
}
#define dbg(...)                                            \
    do {                                                    \
        cerr << __LINE__ << " : " << #__VA_ARGS__ << " = "; \
        dbg0(__VA_ARGS__);                                  \
        cerr << endl;                                       \
    } while (false);
#else
#define dbg(...)
#endif

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    return os << "P(" << p.first << ", " << p.second << ")";
}

template <class T> ostream& operator<<(ostream& os, const V<T>& v) {
    os << "[";
    for (auto d : v) os << d << ", ";
    return os << "]";
}

struct Scanner {
    FILE* fp = nullptr;
    char line[1 << 15];
    size_t st = 0, ed = 0;
    void reread() {
        memmove(line, line + st, ed - st);
        ed -= st;
        st = 0;
        ed += fread(line + ed, 1, (1 << 15) - ed, fp);
    }
    bool succ() {
        while (true) {
            if (st == ed) {
                reread();
                if (st == ed) return false;
            }
            while (st != ed && isspace(line[st])) st++;
            if (st != ed) break;
        }
        if (ed - st <= 50) reread();
        return true;
    }
    template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
    bool read_single(T& ref) {
        if (!succ()) return false;
        while (true) {
            succ();
            size_t sz = 1;
            while (st + sz < ed && !isspace(line[st + sz])) sz++;
            ref.append(line + st, sz);
            st += sz;
            if (st != ed) break;
        }
        return true;
    }
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    bool read_single(T& ref) {
        if (!succ()) return false;
        bool neg = false;
        if (line[st] == '-') {
            neg = true;
            st++;
        }
        ref = T(0);
        while (isdigit(line[st])) {
            ref = 10 * ref + (line[st++] - '0');
        }
        if (neg) ref = -ref;
        return true;
    }
    void read() {}
    template <class H, class... T> void read(H& h, T&... t) {
        bool f = read_single(h);
        assert(f);
        read(t...);
    }
    Scanner(FILE* _fp) : fp(_fp) {}
};

struct Printer {
  public:
    template <bool F> void writeln() { write_single('\n'); }
    template <bool F = false, class H, class... T>
    void writeln(const H& h, const T&... t) {
        if (F) write_single(' ');
        write_single(h);
        writeln<true>(t...);
    }
    Printer(FILE* _fp) : fp(_fp) {}
    ~Printer() { flush(); }

  private:
    static constexpr size_t SIZE = 1 << 15;
    FILE* fp;
    char line[SIZE], small[50];
    size_t pos = 0;
    void flush() {
        fwrite(line, 1, pos, fp);
        pos = 0;
    }
    void write_single(const char& val) {
        if (pos == SIZE) flush();
        line[pos++] = val;
    }
    template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
    void write_single(const T& val) {
        for (char c : val) write_single(c);
    }
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void write_single(T val) {
        if (pos > (1 << 15) - 50) flush();
        if (val == 0) {
            write_single('0');
            return;
        }
        if (val < 0) {
            write_single('-');
            val = -val;  // todo min
        }
        size_t len = 0;
        while (val) {
            small[len++] = char('0' + (val % 10));
            val /= 10;
        }
        reverse(small, small + len);
        memcpy(line + pos, small, len);
        pos += len;
    }
    template <class T> void write_single(const V<T>& val) {
        auto n = val.size();
        for (size_t i = 0; i < n; i++) {
            if (i) write_single(' ');
            write_single(val[i]);
        }
    }
};

#pragma endregion

int main() {
    Scanner sc(stdin);
    Printer pr(stdout);

    int h, w, n;
    sc.read(h, w, n);

    VV<int> g(h, V<int>(w, -TEN(9)));
    V<int> x(n), y(n), z(n);
    for (int i = 0; i < n; i++) {
        sc.read(x[i], y[i], z[i]); x[i]--; y[i]--;
        g[x[i]][y[i]] = z[i];
    }

    for (int ph = 0; ph < 1000; ph++) {
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (i) g[i][j] = max(g[i][j], g[i - 1][j] - 1);
                if (j) g[i][j] = max(g[i][j], g[i][j - 1] - 1);
                if (i < h - 1) g[i][j] = max(g[i][j], g[i + 1][j] - 1);
                if (j < w - 1) g[i][j] = max(g[i][j], g[i][j + 1] - 1);
            }
        }
    }

    for (int i = 0; i < n; i++) {
        if (g[x[i]][y[i]] != z[i]) {
            pr.writeln(string("No"));
            return 0;
        }
    }

    ll sm = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            sm += g[i][j];
        }
    }
    pr.writeln(sm);
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3180
final_rank: 2