We need to understand the problem properly. There is a grid n x n, with a[i][j] coins. Starting at (1,1) and ending at (n,n). Movement allowed: down (i+1,j) or right (i,j+1). The participant collects coins from visited cells? Or something else? It says: "The participant starts at the cell [1][1] and ends at the cell [N][N]. Therefore, a[1][1]=a[N][N]=0. She may move ... When the participant arrives at the cell [N][N] she has to give the guard atleast C number of coins otherwise he will not be able to pass. Your job is to find out what is the maximum number of coins the participant is left with after she passes the final guard."
Interpretation: The participant collects coins along the path? Usually such problems: starting with 0 coins? Or we assume that the participant picks up coins from each cell visited, and at the end must pay at least C coins. We want to maximize the remaining coins after paying C? The phrasing: "maximum number of coins the participant is left with after she passes the final guard." That suggests that she collects coins from the cells she passes, and at the end she must give the guard at least C coins (maybe exactly C? It says "at least C number of coins otherwise he will not be able to pass." So she must give at least C. But to maximize leftover, she would give exactly C if she can, but if she has more than C, she can give exactly C? But the guard demands at least C, so she can give exactly C, leaving the rest. If she has less than C, she cannot pass, output -1.
So we need to compute, for all possible paths from (1,1) to (n,n), the total coins collected along the path. Then, if that total >= C, the leftover is total - C. We want to maximize that leftover (over all paths). If for all paths total < C, then answer is -1.
But careful: "maximum number of coins the participant is left with after she passes the final guard." That would be max over paths of (sum - C) subject to sum >= C. If no path has sum >= C, answer -1.
Alternatively, maybe the participant starts with 0 coins? And collects coins? That seems typical.
But there is a nuance: The guard demands at least C coins, but maybe the participant can choose how many coins to give? But we want to maximize leftover, so give exactly C.
Thus problem reduces: Given n x n grid (with a[1][1]=a[n][n]=0) and move only down/right, find maximum sum of a[i][j] over all paths from (1,1) to (n,n). Then answer = max( max_sum - C, -1?) Actually if max_sum < C, then can't pass => -1. Else answer = max_sum - C? But wait, we need maximum leftover after paying at least C. But the leftover is sum - given. To maximize leftover, we would want to maximize sum, and then give the minimum possible, i.e., exactly C if sum >= C. But is it always optimal to give exactly C? Yes, because leftover = sum - given, and given must be >= C, so to maximize leftover, we set given = C if possible. But can we give more than C? That would decrease leftover, so not optimal. So we want to maximize (sum - C) among paths with sum >= C. So answer = (maximum sum among all paths) - C if that maximum sum >= C? Not exactly: Suppose there is a path with sum S1 < C (cannot use). Among paths with sum >= C, we want max of S - C. That is equivalent to max over all paths S - C, but only if S >= C. So it's max_{paths} (S - C) if that maximum >= -something? Actually S - C is negative if S < C. So if the maximum S among all paths is less than C, then max(S - C) is negative, but the condition to pass is S >= C, so we cannot take negative leftover; you would fail the guard. So answer -1. If the maximum S is >= C, then max_{paths} (S - C) = (max_{paths} S) - C? Wait, need to check: if max S is >= C, then (max S) - C is candidate. But what if there is another path with S slightly less than max S, but S - C may be lower? Since we are subtracting constant C, the maximum of (S - C) is attained at maximum S. So indeed if maximum sum >= C, answer = max_sum - C. If max_sum < C, answer = -1.
Thus the problem reduces to computing the maximum sum path from top-left to bottom-right in an n x n grid with moves down/right. This is a classic DP: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + a[i][j]. Starting dp[1][1] = a[1][1]=0.
But is there any twist? The sample inputs:
Test case 1: n=2, c=7
grid:
0 0
8 0
Paths: from (1,1) to (2,2). Possible paths: (1,1)->(1,2)->(2,2): sum = a[1][1] + a[1][2] + a[2][2] = 0+0+0=0. Or (1,1)->(2,1)->(2,2): sum = 0+8+0=8. So max sum =8. C=7, so leftover = 8-7=1. Output 1. matches.
Test case 2: n=2, c=5
grid:
0 1
3 0
Paths: (1,1)->(1,2)->(2,2): sum=0+1+0=1; (1,1)->(2,1)->(2,2): sum=0+3+0=3; max sum=3 <5 => -1. Output -1. matches.
So indeed it's just max path sum.
But wait, constraints: n up to 100, a[i][j] up to 2500. T up to 20. So straightforward DP works.
But is there any trick like "the guard demands at least C coins" meaning you have to give at least C, but you might also have to pay the guard? The wording: "When the participant arrives at the cell [N][N] she has to give the guard atleast C number of coins otherwise he will not be able to pass." So yes.
Thus answer = max_sum - C if max_sum >= C else -1.
But let's read problem statement again: "Your job is to find out what is the maximum number of coins the participant is left with after she passes the final guard." That's as we reasoned.
But wait, there might be nuance: Could the participant choose not to pick up some coins? Usually they collect automatically when passing through cell. But perhaps they can decide to skip? It says "Each cell has some coins of gold in it". Usually these challenges involve collecting coins when stepping on a cell. It doesn't say you can avoid picking them. So we assume you collect all coins on visited cells.
Thus solution is simple.
But why would this be a challenge? Perhaps there is more: maybe the participant must pay the guard exactly C? But it says atleast C. So we gave exactly C.
Thus solution: For each test case, compute maximum sum along any path from (1,1) to (n,n) moving only down or right. Then output (max_sum - c) if max_sum >= c else -1.
Time: O(n^2) per test case.
Now check constraints: a[i][j] up to 2500, n up to 100, so max sum up to about 2500 * (2n-1)? Actually path length is 2n-1 cells (including start and end). So max sum <= 2500 * 199 ≈ 497,500. So fits in int, but to be safe use long int.
Thus code:
Read T.
Loop T:
read n, c.
read grid n lines.
dp[0][0] based indexing 0 or 1? We'll use 0-index for programming ease. But careful: a[1][1] and a[n][n] are 0 as per input. We'll read all values.
dp[0][0] = a[0][0] (which is 0).
for i from 0 to n-1:
for j from 0 to n-1:
if i==0 and j==0: continue.
up = (i>0) ? dp[i-1][j] : -inf?
left = (j>0) ? dp[i][j-1] : -inf?
dp[i][j] = max(up, left) + a[i][j].
max_sum = dp[n-1][n-1].
if max_sum >= c: print(max_sum - c) else print(-1).
But wait: Could there be a path that doesn't pick up a[1][1]? Since it's start cell, you always start there, so you collect it. And it's 0 anyway. Similarly a[n][n]=0.
This works for sample.
But is there any catch about "the participant starts at cell [1][1] and ends at cell [N][N]. Therefore, a[1][1]=a[N][N]=0." That's given as condition. So input should respect that.
But maybe the participant might have to pay the guard with some coins before collecting? That seems not.
Thus answer is straightforward.
But why would problem be named "Castle Challenge"? Possibly from some contest.
Given the simplicity, we need to be careful about reading input. Also note that there may be multiple test cases. Use fast I/O.
Let's also consider corner cases: n=2 only 2x2, works. n=100, DP works.
Alternatively, could there be a twist: the participant must give at least C coins, but she can also decide to give more? But we maximize leftover, so give exactly C.
Thus final answer.
But wait, I recall a known problem where you have to find maximum coins left after paying a fee that is at least C, but you can choose path such that you have exactly C after paying? Hmm.
Another nuance: The participant may have to pay the guard before leaving, so the leftover is after subtracting payment. So maximizing leftover is as described.
Thus answer.
But let's read problem statement again: "When the participant arrives at the cell [N][N] she has to give the guard atleast C number of coins otherwise he will not be able to pass." So you have to give at least C. You have collected coins along the way. So you must have at least C in your possession to give. If you have more, you can give exactly C, leaving the rest. So yes.
Thus final.
We'll produce solution accordingly.
But wait, there might be a twist: The guard demands at least C, but maybe the participant wants to give exactly C? But there is no penalty for giving more, but it reduces leftover. So to maximize leftover, give exactly C.
Thus solution.
Now implement.
But before finalizing, consider if there is possibility that the participant can avoid collecting coins from some cells? Typically you automatically collect when stepping on. The problem doesn't mention optional collection. So we assume collection is automatic.
Thus solution.
Now produce code.
Implementation details: Use vector<vector> dp(n, vector(n,0)). Or just compute in-place using grid as a, but careful to not overwrite if needed. Could compute DP by iterating rows and columns, using a separate dp array.
Pseudo:
int t;
cin >> t;
while(t--){
int n, c;
cin >> n >> c;
vector<vector> a(n, vector(n));
for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin >> a[i][j];
vector<vector> dp(n, vector(n,0));
dp[0][0] = a[0][0];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==0 && j==0) continue;
int best = INT_MIN;
if(i>0) best = max(best, dp[i-1][j]);
if(j>0) best = max(best, dp[i][j-1]);
dp[i][j] = best + a[i][j];
}
}
int maxSum = dp[n-1][n-1];
if(maxSum >= c) cout << maxSum - c << endl;
else cout << -1 << endl;
}
But note: best might remain INT_MIN if both i==0 and j>0? Actually if i==0, j>0, then only left exists, so best = dp[i][j-1]; similarly if j==0, i>0, only up exists. So we need to handle properly: set best to -inf, then update from valid neighbors. If both invalid, but i=0,j=0 is skipped, so no case. So fine.
But we need to ensure that values are non-negative? a[i][j] >= 0, so dp will be >= 0. So best won't be negative infinite but we can set best = -1e9.
Better: We can use long long to avoid overflow? Actually max sum up to ~500k, safe within int, but with multiple test cases it's fine.
But note: constraints say a[i][j] up to 2500 and n up to 100, maximum path length is 199 cells, max sum = 199*2500=497500, fits in int. However, we might use long for safety.
Now answer.
But the problem might also require to output maximum number of coins left after paying, which could be 0 if max_sum == c. That's fine.
Edge Cases: n=2, c=0. Then you have to give at least 0 coins, so you can always pass, and leftover = max_sum. So answer = max_sum. That's fine.
Edge Cases: c negative? Not possible as per constraints? Not given, but likely c >= 0? It says "atleast C number of coins" so C non-negative integer.
Thus solution.
But wait, there is a nuance: "the participant starts at the cell [1][1] and ends at the cell [N][N]. Therefore, a[1][1]=a[N][N]=0." That's given, but not necessarily enforced in input? They say "Therefore" meaning that they guarantee that? In constraints: a[1][1]=a[N][N]=0. So we can trust.
Alright.
Double-check with sample: Works.
But I recall that sometimes such problems have a twist: the guard demands exactly C coins? But statement says at least C.
Thus final answer.
Now, I'll write a solution explanation.
But there might be something else: Could there be multiple test cases, T up to 20, we need to reset dp each time.
Thus code is straightforward.
Now answer with proper explanation.
However, the problem might be from a contest and there might be trick: The participant may not be forced to collect all coins? Could she skip picking up? The phrasing: "Each cell has some coins of gold in it" but it doesn't explicitly say that she collects them when she steps in. Sometimes such puzzles involve "collect the coins". We assume collection.
Alternatively, if she can choose not to collect, then the optimal would be to collect all because you want as many as possible? But if you need to pay at least C, you might want to collect exactly C? But you want to maximize leftover, so you would collect as many as possible while still paying only C. Actually if you collect more, you have to pay at least C, but you keep the rest, so more is better. So you would collect all possible. So same.
Thus solution stands.
Hence answer.
But wait, could there be a scenario where you want to avoid some coins because you have to pay at least C, and maybe paying more than C is not allowed? But it's allowed to pay at least C, so paying exactly C is allowed. So you always want maximum sum.
Thus all good.
Thus finalize.
Now let's produce final answer: code with DP.
But I recall another possible nuance: The participant must give the guard at least C coins, but perhaps she must give it from the coins collected, and cannot give more than she collected? But that's fine.
Thus.
Now answer.
Given the problem is simple, but we should be careful: The DP can be done using a 2D array, or we can use 1D DP: dp[j] for each row. But 2D is fine.
We'll provide C++ code.
Pseudo:
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
int T; cin>>T;
while(T--){
int n, c; cin>>n>>c;
vector<vector> a(n, vector(n));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
vector<vector> dp(n, vector(n,0));
dp[0][0] = a[0][0];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==0 && j==0) continue;
int best = -1e9;
if(i>0) best = max(best, dp[i-1][j]);
if(j>0) best = max(best, dp[i][j-1]);
dp[i][j] = best + a[i][j];
}
}
int max_sum = dp[n-1][n-1];
if(max_sum >= c) cout << max_sum - c << "\n";
else cout << "-1\n";
}
return 0;
}
But note: a[1][1] and a[n][n] are 0, but we didn't enforce? But input gives them.
Check for boundaries: When i=0 and j>0, dp[0][j] = dp[0][j-1] + a[0][j]; that's correct.
When j=0 and i>0, dp[i][0] = dp[i-1][0] + a[i][0]; correct.
So works.
But maybe we can use a single pass.
Now answer.
However, I should verify with a more complex case: n=3, c=10, grid:
0 5 2
3 4 1
2 0 0
Paths sums: compute max: dp:
(0,0)=0
row0: (0,1)=0+5=5; (0,2)=5+2=7
col0: (1,0)=0+3=3; (2,0)=3+2=5
(1,1): max(dp[0][1]=5, dp[1][0]=3)+4=9
(1,2): max(dp[0][2]=7, dp[1][1]=9)+1=10
(2,1): max(dp[1][1]=9, dp[2][0]=5)+0=9
(2,2): max(dp[1][2]=10, dp[2][1]=9)+0=10
max_sum=10, c=10 => leftover=0. Good.
Thus solution.
Now answer.
But wait, the problem statement says "the participant goes to a room that has n corridors and each corridor has n cells." Corridor = row? So movement is like: can move to same corridor front (i, j+1) or next corridor front (i+1, j). So yes.
Thus done.
Given the simplicity, maybe there is a hidden complication: The participant must give at least C coins, but maybe she can also choose to give some coins before reaching the end? No.
Thus final.
Now I'll produce final answer with explanation and code. We need to compute the maximum total coins that can be collected while moving from the top-left corner (1,1) to the bottom-right corner (n,n) using only moves down or right. The collected sum must be at least C to pay the guard; after paying exactly C (the minimum required), the participant keeps the remainder. The answer is the maximum possible remainder, or -1 if no path collects at least C coins.
This is a classic grid DP problem. Let dp[i][j] be the maximum sum obtainable when reaching cell (i,j). The recurrence is:
dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + a[i][j]
with dp[1][1] = a[1][1] = 0. The answer is dp[n][n] - C if dp[n][n] >= C, else -1.
The constraints (n ≤ 100, values up to 2500) allow a straightforward O(n²) DP per test case.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
int n, c;
cin >> n >> c;
vector<vector<int>> a(n, vector<int>(n));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
cin >> a[i][j];
vector<vector<int>> dp(n, vector<int>(n, 0));
dp[0][0] = a[0][0];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i == 0 && j == 0) continue;
int best = INT_MIN;
if (i > 0) best = max(best, dp[i-1][j]);
if (j > 0) best = max(best, dp[i][j-1]);
dp[i][j] = best + a[i][j];
}
}
int maxSum = dp[n-1][n-1];
if (maxSum >= c)
cout << maxSum - c << '\n';
else
cout << "-1\n";
}
return 0;
}