#include #define ull unsigned ll #define ll long long #define ld long double #define INFL (ll)3e18 #define INF (int)2e9 #define MOD (ll)998244353 #define MODO (ll)(1e9+7) using namespace std; templateistream&operator>>(istream&is,vector&v); templateistream&operator>>(istream&is,tuple& t); templateistream&operator>>(istream&is,pair&p){is>>p.first>>p.second;return is;} templateistream&operator>>(istream&is,vector&v){for(T&in:v)is>>in;return is;} templateistream& operator>>(istream& is, tuple& t) {apply([&](auto&... args) {((is >> args), ...);},t);return is;} int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); int h,w; cin >> h >> w; pair s; cin >> s; int n; cin >> n; vector> sc(n,vector(3)); cin >> sc; vector dp(n); vector ma(n); ll px = s.first,py = s.second; for(int i = 0;i < n;i++){ ll x = sc[i][0],y = sc[i][1],c = sc[i][2]; dp[i] = c; if(i == 0 && !(x == px || y == py || x-y == px-py || x+y == px+py)){ dp[i] = 0; } if(i >= 2) dp[i] = max(dp[i],ma[i-2]+c); if(i > 0 && (x == px || y == py || x-y == px-py || x+y == px+py)){ dp[i] = max(dp[i],dp[i-1]+c); } px = x,py = y; ma[i] = dp[i]; if(i > 0) ma[i] = max(ma[i],ma[i-1]); } cout << ma[n-1] << endl; }