This problem is Spaced Out, Problem 3 from the USACO 2021 January Silver contest.
1#include <iostream>
2#include <algorithm>
3using namespace std;
4
5int main()
6{
7 int n;
8 cin >> n;
9 int grid[1000][1000];
10 for (int i = 0; i < n; i++)
11 {
12 for (int j = 0; j < n; j++)
13 {
14 cin >> grid[i][j];
15 }
16 }
17
18 int rows_alternate = 0, cols_alternate = 0;
19 // each row has an alternating pattern of cow and no cow
20 for (int i = 0; i < n; i++)
21 {
22 int sum[2]{};
23 // there are two ways to alternate, odd index and even index
24 for (int j = 0; j < n; j++)
25 {
26 sum[j % 2] += grid[i][j];
27 }
28 // add the best way to the answer for alternating rows
29 rows_alternate += max(sum[0], sum[1]);
30 }
31
32 // each column has an alternating pattern of cow and no cow
33 for (int i = 0; i < n; i++)
34 {
35 int sum[2]{};
36 // two ways to alternate
37 for (int j = 0; j < n; j++)
38 {
39 sum[j % 2] += grid[j][i];
40 }
41 // add the best way to the answer for alternating columns
42 cols_alternate += max(sum[0], sum[1]);
43 }
44
45 cout << max(rows_alternate, cols_alternate) << endl;
46 return 0;
47}