fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int N, mat[51][51], sum = 0, sumMax = -250000;
  6. cin >> N;
  7. for (int i = 1; i <= N; ++i) {
  8. for (int j = 1; j <= N; ++j) {
  9. cin >> mat[i][j];
  10. }
  11. }
  12. for (int j = N; j >= 1; --j) {
  13. int row = 1, col = j;
  14. while (row <= N && col <= N) {
  15. sum += mat[row][col];
  16. ++row;
  17. ++col;
  18. }
  19. if (sum > sumMax) {
  20. sumMax = sum;
  21. }
  22. sum = 0;
  23. }
  24. for (int i = 2; i <= N; ++i) {
  25. int row = i, col = 1;
  26. while (row <= N && col <= N) {
  27. sum += mat[row][col];
  28. ++row;
  29. ++col;
  30. }
  31. if (sum > sumMax) {
  32. sumMax = sum;
  33. }
  34. sum = 0;
  35. }
  36. for (int i = N - 1; i >= 1; --i) {
  37. int row = 1, col = N - i;
  38. while (row <= N - i && col <= N) {
  39. sum += mat[row][col];
  40. ++row;
  41. --col;
  42. }
  43. if (sum > sumMax) {
  44. sumMax = sum;
  45. }
  46. sum = 0;
  47. }
  48. for (int i = 1; i <= N; ++i) {
  49. int row = i, col = N;
  50. while (row <= N && col <= N) {
  51. sum += mat[row][col];
  52. ++row;
  53. --col;
  54. }
  55. if (sum > sumMax) {
  56. sumMax = sum;
  57. }
  58. sum = 0;
  59. }
  60. cout << sumMax;
  61. return 0;
  62. }
Success #stdin #stdout 0.01s 5236KB
stdin
4 
21 22 35 46
57 62 73 89
19 10 11 12
13 14 15 16
stdout
142