tinycodinglm / cp2 /cp-0012.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
9.83 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. Tools: write_file(path, content) writes...

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. 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-07T09:20:03.537Z

Longest Frugal Stretch

Topic: Two pointers and sliding window | Difficulty: easy

Problem

A hiker records the cost a[i] >= 0 of each of n consecutive days. Find the maximum number of consecutive days whose total cost does not exceed the budget S. If even a single day exceeds S, the answer is 0.

Input

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

Output

Print the maximum length of a contiguous block with total at most S.

Constraints

  • 1 <= n <= 2*10^5
  • 0 <= a[i] <= 10^9
  • 0 <= S <= 10^15

Examples

Example 1

Input:

markdown
6 7
2 1 3 4 1 2

Output:

markdown
3

Note: Days 1..3 cost 6 and days 5..6 cost 3 but are shorter; no block of 4 fits within 7.

Example 2

Input:

markdown
3 0
5 0 0

Output:

markdown
2

Note: Free days still count; the first day alone is too expensive.

Solve this task. Write solutions in Java 17, Go, Ruby, test what you can run on the examples, and send back the final programs. Final answer: code only, no explanation.

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

Writing the Java 17 solution and testing it on the examples.

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

Saving the example inputs.

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

Compiling the Java 17 solution.

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

Running every example.

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

Cross-checking with the Go version.

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

Compiling the Go solution.

Assistant 2026-11-07T09:20:35.832Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T09:20:42.930Z

Cross-checking with the Ruby version.

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

Running every example.

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

All solutions below passed every example.

Java 17

java
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StringTokenizer st = new StringTokenizer(new String(System.in.readAllBytes()));
        int n = Integer.parseInt(st.nextToken());
        long s = Long.parseLong(st.nextToken());
        long[] a = new long[n];
        for (int i = 0; i < n; i++) a[i] = Long.parseLong(st.nextToken());
        long sum = 0;
        int l = 0, best = 0;
        for (int r = 0; r < n; r++) {
            sum += a[r];
            while (sum > s) sum -= a[l++];
            best = Math.max(best, r - l + 1);
        }
        System.out.println(best);
    }
}

Go

go
package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    in := bufio.NewReaderSize(os.Stdin, 1<<20)
    var n int
    var s int64
    fmt.Fscan(in, &n, &s)
    a := make([]int64, n)
    for i := range a {
        fmt.Fscan(in, &a[i])
    }
    var sum int64
    l, best := 0, 0
    for r := 0; r < n; r++ {
        sum += a[r]
        for sum > s {
            sum -= a[l]
            l++
        }
        if r-l+1 > best {
            best = r - l + 1
        }
    }
    fmt.Println(best)
}

Ruby

ruby
n, s = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
sum = 0
l = 0
best = 0
n.times do |r|
  sum += a[r]
  while sum > s
    sum -= a[l]
    l += 1
  end
  best = r - l + 1 if r - l + 1 > best
end
puts best