4857. Olympiad in Hogwarts

 

Hogwarts is hosting its traditional annual Magic Theory Olympiad for first-year students. The school’s caretaker, Argus Filch, has been tasked with assigning students to classrooms.

Each of the four houses has sent its best students to the Olympiad: g students from Gryffindor, s from Slytherin, h from Hufflepuff, and r from Ravenclaw. Filch has m classrooms at his disposal.

Each classroom is enchanted with an expansion charm, so it can accommodate any number of students. However, it must be taken into account that students from the same house, when placed in the same room, might take the opportunity to cheat by sharing ideas on how to solve the tasks. Therefore, the number of students from the same house in any single classroom should be minimized.

We define an optimal seating arrangement as one that minimizes the maximum number of students from the same house in any classroom.

Determine the minimum possible number of students from the same house that Filch will have to place in the same classroom, even under an optimal seating arrangement.

 

Input. The first line contains four integers g, s, h, and r (1 ≤ g, s, h, r ≤ 1000) – the number of students representing Gryffindor, Slytherin, Hufflepuff, and Ravenclaw, respectively.

The second line contains a single integer m (1 ≤ m ≤ 1000) – the number of available classrooms.

 

Output. Print a single integer – the minimum possible number of students from the same house that will have to be placed in the same classroom, even with an optimal arrangement.

 

Sample input 1

Sample output 1

4 3 4 4

2

2

 

 

Sample input 2

Sample output 2

15 14 13 14

5

3

 

 

SOLUTION

conditional statement

 

Algorithm analysis

The largest faculty has mx = max(g, s, h, r) students. If we try to distribute them evenly across m classrooms, then at least one of the classrooms will have no fewer than  students. This is the minimum number of students from a single faculty that will have to be seated in one classroom, even with an optimal arrangement.

 

Algorithm implementation

Read the input data.

 

scanf("%d %d %d %d", &g, &s, &h, &r);

scanf("%d", &m);

 

Compute mx = max(gshr).

 

mx = g;

if (s > mx) mx = s;

if (h > mx) mx = h;

if (r > mx) mx = r;

 

Let’s determine the maximum number of students that may end up in a single classroom: res = .

 

res = mx / m;

if (mx % m > 0) res++;

 

Print the answer.

 

printf("%d\n", res);

 

Java implementation

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int g = con.nextInt();

    int s = con.nextInt();  

    int h = con.nextInt();

    int r = con.nextInt();

    int m = con.nextInt();

   

    int mx = g;

    if (s > mx) mx = s;

    if (h > mx) mx = h;

    if (r > mx) mx = r;

 

    int res = mx / m;

    if (mx % m > 0) res++;

    System.out.println(res);

    con.close();

  }

}

 

Python implementation

Read the input data.

 

a = list(map(int, input().split()))

m = int(input())

 

Compute and print the answer.

 

print((max(a) - 1) // m + 1)