tinycodinglm / cp2 /cp-0002.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
11.1 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-06T23:20:03.537Z

Heavy Left Cuts

Topic: Arrays and prefix sums | Difficulty: easy

Problem

A rope of n segments has integer weights a[1..n] (possibly negative). A cut is placed between two neighbouring segments, so there are n-1 possible cuts. A cut is heavy-left if the total weight of the segments on its left side is strictly greater than the total weight on its right side. Count the heavy-left cuts.

Input

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

Output

Print one integer: the number of heavy-left cuts.

Constraints

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

Examples

Example 1

Input:

markdown
4
3 1 2 4

Output:

markdown
1

Note: Only the cut after the third segment works: 6 > 4.

Example 2

Input:

markdown
1
7

Output:

markdown
0

Note: With one segment there are no cuts.

Example 3

Input:

markdown
3
5 -10 -3

Output:

markdown
1

Solve this task. Write solutions in Java 17, Go, Ruby, 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-06T23:20:05.511Z

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

Assistant 2026-11-06T23:20:09.870Z

Saving the example inputs.

3 tool calls (write_file)
Assistant 2026-11-06T23:20:18.432Z

Compiling the Java 17 solution.

Assistant 2026-11-06T23:20:22.635Z

Running every example.

3 tool calls (run_shell)
Assistant 2026-11-06T23:20:31.785Z

Cross-checking with the Go version.

Assistant 2026-11-06T23:20:35.832Z

Compiling the Go solution.

Assistant 2026-11-06T23:20:40.427Z

Running every example.

3 tool calls (run_shell)
Assistant 2026-11-06T23:20:48.561Z

Cross-checking with the Ruby version.

Assistant 2026-11-06T23:20:53.000Z

Running every example.

3 tool calls (run_shell)
Assistant 2026-11-06T23:21:01.722Z

Approach. Compute the total sum, then sweep the cuts keeping the running left sum; the right sum is total minus left.

Correctness. For cut i the left sum is the prefix sum of the first i values and the right sum is the total minus it, so the comparison is exact.

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

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 {
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        st.nextToken();
        int n = (int) st.nval;
        long[] a = new long[n];
        long total = 0;
        for (int i = 0; i < n; i++) {
            st.nextToken();
            a[i] = (long) st.nval;
            total += a[i];
        }
        long left = 0;
        int cnt = 0;
        for (int i = 0; i < n - 1; i++) {
            left += a[i];
            if (left > total - left) cnt++;
        }
        System.out.println(cnt);
    }
}

Go

go
package main

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

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

Ruby

ruby
n = gets.to_i
a = gets.to_s.split.map(&:to_i)
total = a.sum
left = 0
cnt = 0
(0...n - 1).each do |i|
  left += a[i]
  cnt += 1 if left > total - left
end
puts cnt