#C5309. Sum of Unique Numbers in a Grid
Sum of Unique Numbers in a Grid
Sum of Unique Numbers in a Grid
You are given a two-dimensional grid with N rows and M columns. Each cell in the grid contains an integer. Your task is to compute the sum of all unique integers in the grid. In other words, even if a number appears multiple times in the grid, it should be summed only once.
The problem can be mathematically described as follows:
Let \( G \) be an \( N \times M \) grid, and let \( S \) be the set of distinct elements in \( G \). Compute: \[ \text{result} = \sum_{x \in S} x \]
You need to read the grid from the standard input and output the computed sum to the standard output.
inputFormat
The first line of input contains two space-separated integers N
and M
denoting the number of rows and columns respectively. The following N
lines each contain M
space-separated integers representing the grid.
outputFormat
Output a single integer representing the sum of all distinct values in the grid.
## sample3 3
1 2 3
4 1 5
6 7 2
28
</p>