fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6. short length;
  7. short counter=0;
  8. short which=0;
  9. int largest_sum=-10000000;
  10. int sum=0;
  11.  
  12. cin>>length;
  13. //matrix declaration
  14. short **matrix = new short*[length];
  15.  
  16. for(short row=0; row<length; row++){
  17. matrix[row] = new short [length];
  18. }
  19. //filling the matrix
  20. for(short row=0; row<length; row++){
  21. for(int column=0; column<length; column++){
  22. cin>>matrix[row][column];
  23. }
  24. }
  25. //calculating the sum of diagonals and choosing the largest one (half of all diagonals)
  26. for(short row=length-1; row>=0; row--){
  27. short r=row;
  28. short c=0;
  29. while(r<length){
  30. sum+=matrix[r][c];
  31. r=r+1;
  32. c=c+1;
  33. }
  34. ++counter;
  35.  
  36. if(sum>largest_sum){
  37. largest_sum=sum;
  38. which=counter;
  39. }
  40. sum=0;
  41. }
  42. //calculating the sum of diagonals and choosing the largest one (second half of all diagonals)
  43. for(short row=1; row<length; row++){
  44. short r=0;
  45. short c=row;
  46. while(c<length){
  47. sum+=matrix[r][c];
  48. r=r+1;
  49. c=c+1;
  50. }
  51. ++counter;
  52. if(sum>largest_sum){
  53. largest_sum=sum;
  54. which=counter;
  55. }
  56. sum=0;
  57. }
  58. //removing from memory
  59. for(short i=0; i<length; i++){
  60. delete [] matrix[i];
  61. }
  62. delete [] matrix;
  63. //score
  64. cout<<which<<" "<<largest_sum;
  65.  
  66. return 0;
  67. }
Success #stdin #stdout 0s 4524KB
stdin
4
1 2 3 9
8 1 3 2
1 0 1 1
0 7 0 3
stdout
7 9