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

Unit Gig Planner

Topic: Sorting and greedy | Difficulty: medium

Problem

A freelancer can do one gig per day, each gig takes exactly one day, and days are numbered 1, 2, 3, .... Gig i pays p_i if it is done on some day not later than its deadline d_i; otherwise it pays nothing and is skipped. Each gig is done at most once and at most one gig per day. Print the maximum total payment.

Input

The first line contains n. Each of the next n lines contains d_i and p_i.

Output

Print one integer: the maximum total payment.

Constraints

  • 1 <= n <= 2*10^5
  • 1 <= d_i <= 10^9
  • 1 <= p_i <= 10^9

Examples

Example 1

Input:

markdown
5
2 100
1 19
2 27
1 25
3 15

Output:

markdown
142

Note: Take 100 and 27 on days 1-2 (in some order) and 15 on day 3; the deadline-1 gigs cannot also fit.

Example 2

Input:

markdown
3
1000000000 5
1 9
1 7

Output:

markdown
14

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

Assistant 2026-11-08T18:20:05.511Z

Writing the Go solution and testing it on the examples.

Assistant 2026-11-08T18:20:09.870Z

Saving the example inputs.

2 tool calls (write_file)
Assistant 2026-11-08T18:20:16.536Z

Compiling the Go solution.

Assistant 2026-11-08T18:20:20.465Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-08T18:20:27.386Z

Cross-checking with the Kotlin version.

Assistant 2026-11-08T18:20:31.785Z

Compiling the Kotlin solution.

Assistant 2026-11-08T18:20:35.832Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-08T18:20:42.930Z

Cross-checking with the Java 17 version.

Assistant 2026-11-08T18:20:46.547Z

Compiling the Java 17 solution.

Assistant 2026-11-08T18:20:50.712Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-08T18:20:57.987Z

All solutions below passed every example.

Go

go
package main

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

var par []int

func find(x int) int {
    for par[x] != x {
        par[x] = par[par[x]]
        x = par[x]
    }
    return x
}

func main() {
    rd := bufio.NewReaderSize(os.Stdin, 1<<20)
    var n int
    fmt.Fscan(rd, &n)
    type job struct{ d, p int }
    js := make([]job, n)
    for i := range js {
        fmt.Fscan(rd, &js[i].d, &js[i].p)
        if js[i].d > n {
            js[i].d = n
        }
    }
    sort.Slice(js, func(a, b int) bool { return js[a].p > js[b].p })
    par = make([]int, n+1)
    for i := range par {
        par[i] = i
    }
    total := 0
    for _, j := range js {
        s := find(j.d)
        if s > 0 {
            total += j.p
            par[s] = s - 1
        }
    }
    fmt.Println(total)
}

Kotlin

kotlin
import java.io.*
import java.util.*

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val n = br.readLine().trim().toInt()
    val d = IntArray(n)
    val p = LongArray(n)
    for (i in 0 until n) {
        val st = StringTokenizer(br.readLine())
        d[i] = minOf(st.nextToken().toLong(), n.toLong()).toInt()
        p[i] = st.nextToken().toLong()
    }
    val idx = (0 until n).sortedByDescending { p[it] }
    val par = IntArray(n + 1) { it }
    fun find(x0: Int): Int {
        var x = x0
        while (par[x] != x) {
            par[x] = par[par[x]]
            x = par[x]
        }
        return x
    }
    var total = 0L
    for (i in idx) {
        val s = find(d[i])
        if (s > 0) {
            total += p[i]
            par[s] = s - 1
        }
    }
    println(total)
}

Java 17

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

public class Main {
    static int[] par;
    static int find(int x) {
        while (par[x] != x) {
            par[x] = par[par[x]];
            x = par[x];
        }
        return x;
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine().trim());
        long[][] js = new long[n][2];
        for (int i = 0; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            js[i][0] = Math.min(Long.parseLong(st.nextToken()), n);
            js[i][1] = Long.parseLong(st.nextToken());
        }
        Arrays.sort(js, (a, b) -> Long.compare(b[1], a[1]));
        par = new int[n + 1];
        for (int i = 0; i <= n; i++) par[i] = i;
        long total = 0;
        for (long[] j : js) {
            int s = find((int) j[0]);
            if (s > 0) {
                total += j[1];
                par[s] = s - 1;
            }
        }
        System.out.println(total);
    }
}