tinycodinglm / cp2 /cp-0006.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
3.87 kB
cp-practice-sim trace
System You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Reply with code only; no explanation.

You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Reply with code only; no explanation.

User 2026-11-07T03:20:03.537Z

Greenhouse Square Yield

Topic: Arrays and prefix sums | Difficulty: medium

Problem

A greenhouse is an n by m grid of plots; plot (i, j) has a signed yield g[i][j] (negative means a net loss). A sprinkler covers a k by k square of plots aligned with the grid. Find the maximum total yield over all placements of the square that lie fully inside the grid.

Input

The first line contains n, m and k. Each of the next n lines contains m integers.

Output

Print the maximum total yield of a k by k square.

Constraints

  • 1 <= n, m and nm <= 210^5
  • 1 <= k <= min(n, m)
  • |g[i][j]| <= 10^9

Examples

Example 1

Input:

markdown
3 3 2
1 2 3
4 5 6
7 8 9

Output:

markdown
28

Example 2

Input:

markdown
2 2 1
-3 -1
-4 -2

Output:

markdown
-1

Note: All values are negative, so the answer is the largest single cell.

Solve this in JavaScript (Node.js), Ruby, C#, one complete program per language. Code only, no explanation.

Assistant 2026-11-07T03:20:05.511Z

JavaScript (Node.js)

javascript
const t = require('fs').readFileSync(0, 'utf8').split(/\s+/).filter(x => x.length).map(Number);
let p = 0;
const n = t[p++], m = t[p++], k = t[p++];
const w = m + 1;
const S = new Float64Array((n + 1) * w);
for (let i = 1; i <= n; i++) {
  for (let j = 1; j <= m; j++) {
    S[i * w + j] = t[p++] + S[(i - 1) * w + j] + S[i * w + j - 1] - S[(i - 1) * w + j - 1];
  }
}
let best = -Infinity;
for (let i = k; i <= n; i++) {
  for (let j = k; j <= m; j++) {
    const v = S[i * w + j] - S[(i - k) * w + j] - S[i * w + j - k] + S[(i - k) * w + j - k];
    if (v > best) best = v;
  }
}
console.log(String(best));

Ruby

ruby
t = STDIN.read.split.map(&:to_i)
n, m, k = t[0], t[1], t[2]
p = 3
s = Array.new(n + 1) { Array.new(m + 1, 0) }
(1..n).each do |i|
  (1..m).each do |j|
    s[i][j] = t[p] + s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1]
    p += 1
  end
end
best = nil
(k..n).each do |i|
  (k..m).each do |j|
    v = s[i][j] - s[i - k][j] - s[i][j - k] + s[i - k][j - k]
    best = v if best.nil? || v > best
  end
end
puts best

C#

csharp
using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        var t = Console.In.ReadToEnd().Split((char[])null, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();
        int p = 0;
        int n = (int)t[p++], m = (int)t[p++], k = (int)t[p++];
        var s = new long[n + 1, m + 1];
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                s[i, j] = t[p++] + s[i - 1, j] + s[i, j - 1] - s[i - 1, j - 1];
        long best = long.MinValue;
        for (int i = k; i <= n; i++)
            for (int j = k; j <= m; j++)
            {
                long v = s[i, j] - s[i - k, j] - s[i, j - k] + s[i - k, j - k];
                if (v > best) best = v;
            }
        Console.WriteLine(best);
    }
}