Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define wt while(t--)
- #define fast ios_base::sync_with_stdio(false)
- #define pb push_back
- #define pob pop_back
- #define pf push_front
- #define pof pop_front
- #define mp make_pair
- #define INF -1000000000000000
- typedef unsigned long long int ull;
- typedef long long int ll;
- using namespace std;
- ll b[505][505];
- ll p[505][505];
- ll dp[505][505];
- bool visited[505][505];
- ll n;
- bool isSafe(ll i,ll j)
- {
- if(i>0&&i<=n&&j>0&&j<=n)
- return true;
- return false;
- }
- ll recur(ll i,ll j)
- {
- if(dp[i][j]!=-1)
- return dp[i][j];
- if(visited[i][j]==true)
- return 0;
- else
- {
- visited[i][j]=true;
- if(i==n&&j==n)
- return dp[i][j]=b[i][j];
- ll k = b[i][j];
- ll a,c;
- a = c = INF;
- if(isSafe(i+1,j))
- {
- if(p[i+1][j]==1)
- a = recur(i+1,j);
- }
- if(isSafe(i,j+1))
- {
- if(p[i][j+1]==1)
- c = recur(i,j+1);
- }
- return dp[i][j]=k+max(a,c);
- }
- }
- int main()
- {
- fast;
- memset(p,0,sizeof(p));
- memset(dp,-1,sizeof(dp));
- memset(visited,false,sizeof(visited));
- ll q,i,j,k,l,r,x,ans;
- cin>>n>>q;
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=n;j++)
- cin>>b[i][j];
- }
- x = 1;
- while(q--)
- {
- cin>>i>>j>>k;
- for(l = max(x,i-k);l<=min(i+k,n);l++)
- {
- for(r = max(x,j-k);r<=min(j+k,n);r++)
- {
- if(abs(l-i)+abs(r-j)<=k)
- p[l][r]=1;
- }
- }
- }
- ans = recur(1,1);
- if(visited[n][n]==true)
- {
- cout<<"YES\n";
- cout<<ans<<endl;
- }
- else
- cout<<"NO\n";
- return 0;
- }
Add Comment
Please, Sign In to add comment