Mind the Traps (APCS 2025-06 Beginner)

Points 100 1.0s 256M

A simulation game is played on a one-dimensional number line, with the following rules:

  1. The starting position is \(0\).
  2. The initial health is \(k\).
  3. When the current health is \(v\), the character jumps \(v\) cells to the right, i.e. from position \(p\) to \(p + v\).
  4. After each move, if it lands on a multiple of \(x_1\) or \(x_2\), health is lost:
    • If it lands on a multiple of \(x_1\), health decreases by \(y_1\).
    • If it lands on a multiple of \(x_2\), health decreases by \(y_2\).
    • If it is a multiple of both \(x_1\) and \(x_2\), health decreases by \(y_1 + y_2\) in total.
  5. When health becomes \(\le 0\), the game ends, and the current position is output.

Input

The first line contains an integer \(k\), the initial health.

The second line contains two integers \(x_1\) and \(y_1\).

The third line contains two integers \(x_2\) and \(y_2\).

Constraints

  • \(1 \le k \le 20\)
  • \(1 \le x_1, x_2 \le 10\)
  • \(1 \le y_1, y_2 \le 20\)

Output

Output the position where the game ends.

Scoring

The time limit for each test case is \(1\) second; your score is the sum over the test cases you pass. The subtasks are:

  • Subtask 1 (\(40\) points): \(x_1 = 3\), \(x_2 = 4\), \(y_1 = y_2 = k\).
  • Subtask 2 (\(60\) points): no additional constraints.

Sample Input 1

1
3 1
4 1

Sample Output 1

3

Sample Input 2

7
3 2
2 3

Sample Output 2

18

Source

APCS programming exam, June 2025, Problem 1.

Problem page help

Keyboard shortcuts

Main features

  • Sample tests — Runs the sample cases bundled with the problem and auto-compares against the expected output.
  • Custom test — Run your code with your own stdin. Optionally tick the "Compare with expected (diff)" box to verify against expected output line-by-line.
  • Template — Paste the default code template you set on your profile page.
  • Collab — Edit this problem together with classmates in real time.
  • Auto-draft — Editor contents auto-save to your browser every 1.5 seconds (per account / problem / language).
  • Submit — Send your code to the judge for grading; returns AC / WA / TLE etc.

Limits

  • Source code: at most 65,536 characters
  • Custom test stdin and expected output: at most 1 MB each (≈1 million characters)
  • Custom test and sample test share the sandbox; about 1 request per 3 s per user (sample test: 1 per 1 s)
  • Custom test and sample test both have a 15 second wall-clock cap (the official judge still uses the problem time limit)
  • Interactive problems do not offer custom test (cannot simulate interaction with the judge).
  • Submitting has no rate limit, but rapid repeated submissions on the same problem are treated as score farming.