tinycodinglm / cp2 /cp-0007.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
10.2 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. Give only a concise rationale: approach, correctness idea, and time/space com...

You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space complexity. Tools: write_file(path, content) writes a UTF-8 file (creating directories); run_shell(command) runs a bash command and returns its output and exit status. Test solutions on the provided examples before answering.

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

Long Enough Streak

Topic: Arrays and prefix sums | Difficulty: medium

Problem

A trader logs n daily profits a[1..n] (negative means a loss). A reporting window is a contiguous block of days and must contain at least L days. Find the maximum possible total profit over all valid windows.

Input

The first line contains n and L. The second line contains n integers a[1..n].

Output

Print one integer: the maximum window total.

Constraints

  • 1 <= L <= n <= 2*10^5
  • |a[i]| <= 10^9

Examples

Example 1

Input:

markdown
5 2
1 -5 4 -1 3

Output:

markdown
6

Note: The window days 3..5 has total 4-1+3 = 6.

Example 2

Input:

markdown
4 1
-2 -1 -3 -4

Output:

markdown
-1

Note: Every value is negative, so the best window is the single day with -1.

Solve this task. Write solutions in TypeScript, Python 3, Rust, test what you can run on the examples, and send back the final programs. Include a concise rationale (approach, correctness idea, time/space complexity).

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

Writing the TypeScript solution and testing it on the examples.

Assistant 2026-11-07T04:20:09.870Z

Saving the example inputs.

2 tool calls (write_file)
Assistant 2026-11-07T04:20:16.536Z

Compiling the TypeScript solution.

Assistant 2026-11-07T04:20:20.465Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T04:20:27.386Z

Cross-checking with the Python 3 version.

Assistant 2026-11-07T04:20:31.785Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T04:20:38.061Z

Cross-checking with the Rust version.

Assistant 2026-11-07T04:20:42.930Z

Compiling the Rust solution.

Assistant 2026-11-07T04:20:46.547Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T04:20:53.000Z

Approach. With prefix sums P, a window ending at day i with length at least L has total P[i] - P[j] for some j <= i-L. Sweep i from L to n while maintaining the minimum of P[0..i-L].

Correctness. For fixed i, the best valid start is exactly the j in [0, i-L] minimising P[j], and the running minimum is updated before it is used.

Complexity. Time: O(n). Space: O(n).

All solutions below passed every example.

TypeScript

typescript
declare const require: any;
const t: number[] = require('fs').readFileSync(0, 'utf8').split(/\s+/).filter((x: string) => x.length).map(Number);
const n = t[0], L = t[1];
const P: number[] = new Array(n + 1).fill(0);
for (let i = 1; i <= n; i++) P[i] = P[i - 1] + t[1 + i];
let mn = Infinity, best = -Infinity;
for (let i = L; i <= n; i++) {
  mn = Math.min(mn, P[i - L]);
  best = Math.max(best, P[i] - mn);
}
console.log(String(best));

Python 3

python
import sys
t = sys.stdin.read().split()
n, L = int(t[0]), int(t[1])
P = [0] * (n + 1)
for i in range(1, n + 1):
    P[i] = P[i - 1] + int(t[1 + i])
mn = float('inf')
best = None
for i in range(L, n + 1):
    mn = min(mn, P[i - L])
    v = P[i] - mn
    if best is None or v > best:
        best = v
print(best)

Rust

rust
use std::io::*;
fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    let t: Vec<i64> = s.split_whitespace().map(|x| x.parse().unwrap()).collect();
    let n = t[0] as usize;
    let l = t[1] as usize;
    let mut p = vec![0i64; n + 1];
    for i in 1..=n {
        p[i] = p[i - 1] + t[1 + i];
    }
    let mut mn = i64::MAX;
    let mut best = i64::MIN;
    for i in l..=n {
        mn = mn.min(p[i - l]);
        best = best.max(p[i] - mn);
    }
    println!("{}", best);
}