Sum of Array

Sum of Array

Easy PHP PHP Arrays 42 views
Explanation Complexity

Problem Statement

Given n integers, print their sum.

Input Format

First n. Next line n integers.

Output Format

One integer sum.

Example

5
1 2 3 4 5
15

Constraints

n

Input / Output Format

Input Format
First n. Next line n integers.
Output Format
One integer sum.
Constraints
n

Examples

Input:
5 1 2 3 4 5
Output:
15

Example Solution (Public)

PHP
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$tokens=preg_split('/\\s+/', $inputText);
$i=0; $n=intval($tokens[$i++] ?? 0);
$sum=0;
for($k=0;$k<$n;$k++) $sum += intval($tokens[$i++] ?? 0);
echo $sum;
?>

Official Solution Code

<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$tokens=preg_split('/\\s+/', $inputText);
$i=0; $n=intval($tokens[$i++] ?? 0);
$sum=0;
for($k=0;$k<$n;$k++) $sum += intval($tokens[$i++] ?? 0);
echo $sum;
?>
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.