I solve similar problems for selection for an internship at Yandex, I think I solved the problem, but it gives out WA (Wrong Answer)
Conditions: 1. The amount of various
Time limit 2 seconds
Memory limit 256Mb
Input standard input or input.txt
Output standard output or output.txt
You need to send the source code of the program that solves the task.
Given an array a of n integers. Write a program that displays the sum of the various numbers in the array.
Input format
The first line of the input contains the number n (1 ≤ n ≤ 100 000). The second line contains n integers ai (0 ≤ ai ≤ 1 000 000 000).
Output format
Print the single number s, the sum of the various numbers in the array a. Be careful in choosing which integer data type to use.
Example 1
Input
3
3 5 4
Conclusion
12
Example 2
Input
five
5 5 5 5 5
Conclusion
five
Example 3
Input
ten
7 10 3 2 7 4 8 5 9 10
Conclusion
48
My code is:
import Foundation var arrayCount: Int? = Int(readLine() ?? "0") var arrayOfElements: [Int] = [] for _ in 0...(arrayCount ?? 0) { let element: Int? = Int(readLine() ?? "") arrayOfElements.append(element ?? 0) } var setOfElements: Set = Set(arrayOfElements) print(setOfElements.reduce(0, +))