HardObjectsNot started

Seat inventory events

Each venue begins with a known seat count and capacity. Process signed booking changes in order. Reject an event for an unknown venue, a negative resulting count, or a count above capacity; rejected events must not mutate state. Print final counts sorted by venue ID and the number of rejected events.

Input

First line N; then N id capacity occupied rows. Next line M; then M id delta rows.

Output

Sorted id occupied lines followed by rejected X.

Example 1

Input

2
a 10 3
b 5 1
4
a 4
b 5
x 1
a -9

Output

a 7
b 1
rejected 3

The first event raises A to seven; B would exceed capacity, X is unknown, and the final A event would go negative, so three are rejected.

Constraints

  • 1 <= N <= 100 venues and 0 <= M <= 300 events.
  • Initial occupied seats are within 0..capacity; each delta is a signed integer.

Hints

Hint 1 of 2

Keep capacity and occupied count together per venue.

Hint 2 of 2

Validate the proposed next count before assigning it.

Solution

Show a reference solution and explanation
 JavaScript · reference solution
const n = Number(readline());
const state = new Map();
for (let i = 0; i < n; i++) {
  const [id, cap, used] = readline().split(' ');
  state.set(id, { cap: Number(cap), used: Number(used) });
}
const m = Number(readline());
let rejected = 0;
for (let i = 0; i < m; i++) {
  const [id, delta] = readline().split(' ');
  const item = state.get(id);
  const next = item ? item.used + Number(delta) : -1;
  if (!item || next < 0 || next > item.cap) rejected++;
  else item.used = next;
}
for (const id of [...state.keys()].sort()) console.log(id, state.get(id).used);
console.log('rejected', rejected);

Why it works

The Map is the current source of truth. For each event, calculate a proposed count and commit it only when the venue exists and the result is within 0..capacity. Sorting keys only for output gives stable reporting without changing event order.

Lesson for this exercise: JavaScript Objects: Properties, Methods and Nesting

Your program
const n = Number(readline());
const state = new Map();
for (let i = 0; i < n; i++) {
  const [id, cap, used] = readline().split(' ');
  state.set(id, { cap: Number(cap), used: Number(used) });
}
const m = Number(readline());
let rejected = 0;
// Process events before printing sorted balances and rejected count.

Tests: 3 cases including the examples. Passing every test marks the exercise solved in this browser.

How this page was checked. Every program on it was run with Node.js 22 at build time; runs in your browser in an isolated Web Worker by the publishing checks, and the output shown is what it printed. The Run buttons execute the same code on your device; nothing you type is sent anywhere.