package lab5;

import java.util.Scanner;

public class Example7 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        // ===== Timus 1319 — Hotel =====
        System.out.print("Timus 1319. Enter n: ");
        int n = in.nextInt();

        int[][] hotel = new int[n][n];
        int value = 1;

        for (int col = 0; col < n; col++) {
            for (int row = 0; row <= col; row++) {
                hotel[row][col - row] = value++;
            }
        }
        for (int col = 1; col < n; col++) {
            for (int row = col; row < n; row++) {
                hotel[row][n - 1 - (row - col)] = value++;
            }
        }

        System.out.println("Hotel matrix:");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(hotel[i][j] + " ");
            }
            System.out.println();
        }

        // ===== Timus 1787 — Turn for MEGA =====
        System.out.print("Timus 1787. Enter k and n: ");
        int k = in.nextInt();
        int m = in.nextInt();

        int cars = 0;
        for (int i = 0; i < m; i++) {
            cars += in.nextInt();
            if (cars > k) {
                cars -= k;
            } else {
                cars = 0;
            }
        }

        System.out.println("Cars left in queue: " + cars);

        in.close();
    }
}
