A dockyard clerk keeps a manifest listing the weight (in kilograms) of every crate loaded onto a vessel. As a quick sanity check against transcription errors, the clerk also computes a "digit checksum": for every crate, sum the individual digits of its weight, then add all of those per-crate digit sums together. The clerk wants to know how far apart the two totals are — the real total weight versus the digit checksum total — so any large discrepancy can be flagged for review.
Given the list of crate weights, compute the absolute difference between the sum of the weights and the sum of the digit sums of the weights.
Line 1: a single integer n, the number of crates. Line 2: n space-separated integers w_1 ... w_n, the weight of each crate.
A single integer: the absolute difference between the total weight and the total digit-sum checksum.
Example 1
Input
4 1 15 6 3
Expected
9
Explanation
Total weight = 1+15+6+3 = 25. Digit-sum checksum = digitSum(1)+digitSum(15)+digitSum(6)+digitSum(3) = 1+6+6+3 = 16. |25-16| = 9.
Example 2
Input
4 1 2 3 4
Expected
0
Explanation
Total weight = 1+2+3+4 = 10. Every weight here is a single digit, so the digit-sum checksum equals the same values: 1+2+3+4 = 10. |10-10| = 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →