EasyArraysNot started

Parcel weights summary

A courier scans every parcel loaded onto a van and records its weight in grams. The scanner sends all the weights on one line. Print the total weight of the load and the weight of the heaviest parcel.

Input

One line: 1 to 1000 integers from 0 to 50000 separated by single spaces.

Output

Two lines: Total: <sum> and Heaviest: <max>.

Example 1

Input

1200 350 4800

Output

Total: 6350
Heaviest: 4800

1200 + 350 + 4800 = 6350, and 4800 is the largest value.

Example 2

Input

900

Output

Total: 900
Heaviest: 900

With a single parcel the total and the heaviest are the same number.

Constraints

  • 1 <= number of parcels <= 1000
  • 0 <= weight <= 50000

Hints

Hint 1 of 3

explode(' ', $line) gives an array of strings; array_map('intval', ...) turns them all into integers.

Hint 2 of 3

PHP has array_sum() and max() built in; you do not need to write a loop.

Hint 3 of 3

Print each label and value with echo, ending each line with "\n".

Solution

Show a reference solution and explanation
 PHP · reference solution
<?php
$weights = array_map('intval', explode(' ', trim(fgets(STDIN))));
echo "Total: ", array_sum($weights), "\n";
echo "Heaviest: ", max($weights), "\n";

Why it works

Turning the text line into an array of integers is the important step, because array_sum() and max() then do the rest. array_map('intval', $parts) applies the built-in intval function to every element and returns a new array, which is the idiomatic PHP way to convert a list of strings. The starter already contains that line, so the exercise is about knowing which built-ins to reach for. Writing your own loop with a running total and a running maximum is also correct and is worth doing once to see what the built-ins hide.

Lesson for this exercise: Arrays in PHP

Your program
<?php
$weights = array_map('intval', explode(' ', trim(fgets(STDIN))));
// print the total weight and the heaviest parcel
Run is not available for PHP in the browser yet. Write your program here, then download it and run it locally with PHP 8.4 against the examples above. The reference solution below was verified the same way.

Tests: 5 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 PHP 8.4 at build time by the publishing checks, and the output shown is what it printed. Running PHP inside the browser is not available yet, so the Run button is absent rather than pretending; copy the code and run it with PHP 8.4 locally.