Sum of max elements in sub-triangles

Here is a solution that can be made O(n^2 log(k)) which is fast enough.

The idea is this. Going from the nxn triangle of triangles of size 1 to the (n-1)x(n-1) triangle of max values of triangles of size 2 is a O(n) operation. Just compare each triangle to the max of its neighbors.

The same trick can be used to go from that second triangle to the (n-2)x(n-2) triangle of triangles of size 2. But instead if you skip one in each direction, you can get directly to the (n-3)x(n-3) triangle of max values in triangles of size 4. Also in time O(n). To illustrate the latter suppose that we started with:

    2
   3 1
  1 2 4
 4 2 1 5
6 1 4 2 3

To get to the size 2 triangles we compare each triangle to its neighbors.

   3
  3 4
 4 2 5
6 4 4 5

And to get to the size 4 triangle compare skipping one, so the bottom one we compare 6, 3, 4. The next over we compare 4, 4, 5 and so on. To get:

 5
6 5

And then we add them together to get 11.

Next, from the (n-3)x(n-3) triangle of max values in triangles of size 4 you can go directly to the triangle of max values in triangles of sizes 5, 6, 7, or 8 by choosing the size of the triangles we'll compare to be next, skip 1, skip 2 or skip 3.

And so on resulting in O(log(k)) steps to get the triangle of max values in k by k triangles.