I don't get what is wrong with my program.

So the site of our university ,even though the results in my program are right , for some weird reason it does not approve of my program. If anyone knows the reason please explain it to me. Here is the exercise:
Task A: Maximum yield
The field is a rectangle of size N on M, divided into cells by 1 x 1. The combine starts to move from the upper left corner of the field (cell with coordinates (0,0)) to the lower right corner (cell with coordinates (N-1, M- 1)). At the same time, the harvester can move from the current cell only to a cage located either to the right or below the current one. The combine can not go beyond the boundaries of the field.

Being in a cage with coordinates (x, y), the harvester collects Kxy tons of harvest. The combine must be passed in such a way as to assemble the maximum possible yield. What will be the harvest in this case?

Input
The first line of the standard input stream contains the number of test cases T. Each test case is a description of the field.

The field description consists of several lines. The first line contains two integers N and M - the size of the field (1 ≤ N, M ≤ 100).

Each of the N subsequent lines contains M Kxy integers separated by spaces (0 ≤ Kxy ≤ 100).

Output
For each test case, you need to output a single number of the standard output stream into a separate line - the maximum harvest collected by the combine.
Examples:
INPUT:
2//T test cases
2 2 // N and M
9 10
7 10
5 3
8 8 5
8 6 8
1 9 10
8 2 3
1 3 1
OUTPUT
29
45

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  #include <iostream>

using namespace std;

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int N,M;
        cin >> N >> M;
        int field[100][100];
        for ( int  i = 0; i < N; i++)
        {
            for ( int  j = 0; j < M; j++)
            {
                cin >> field[i][j];
            }
        }
      int   CombHarv = 0;
      int NewField[100][100];
      NewField[0][0] = field[0][0];
      for (int i = 1; i < N; i++)
      {
          NewField[i][0] = field[i][0] + field[i-1][0];
      }
      for ( int  i = 1; i < M; i++)
      {
          NewField[0][i] = field[0][i] + field[0][i-1];
      }
      for ( int i = 1; i < N; i++)
        {
            for ( int  j = 1; j < M; j++)
            {
                if ( NewField[i-1][j] > NewField[i][j-1])
                {
                  NewField[i][j] = field[i][j] + NewField[i-1][j];
                }
                else
                {
                    NewField[i][j] = field[i][j] + NewField[i][j-1];
                }
            }
          }
          CombHarv = NewField[N-1][M-1];
          cout << CombHarv << endl;
    }
    return 0;
}
So the site of our university ,even though the results in my program are right , for some weird reason it does not approve of my program. If anyone knows the reason please explain it to me.


That weird reason is because your algorithm is not correct for all possible input. One of the things you need to do for such assignments is come up with situations or sets of data that will confound your algorithm. Then, you need to patch up your algorithm to get those working.

For instance, your algorithm produces 42 for this input:
1
4 4
1 1 1 10
1 9 1 10
1 1 1 10
1 1 1 10

But, the optimal output is 43.

Topic archived. No new replies allowed.