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
$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