Tổng mảng kì lạ

View as PDF

Submit solution

Points: 0.01
Time limit: 1.0s
Memory limit: 64M
Input: stdin
Output: stdout

Author:
Problem type

Cho một mảng có ~n~ giá trị, ~a_0, a_1, ..., a_{n - 1}~. Mentor Popi muốn các bạn thực hiện tính toán trên mảng này theo quy tắc như sau $$S = a_0 + a_1 - a_2 + a_3 - a_4 + \text{...} \pm a_{n-1}$$ Ví dụ: ~n = 5~, ~a = [1, 2, 3, 4, 5]~ ~\implies S = 1 + 2 - 3 + 4 - 5 = -1~

Input
  • Dòng đầu tiên, gồm duy nhất một số nguyên ~n~ là kích thước của mảng.
  • Tiếp theo, gồm ~n~ giá trị ~a_0, a_1, ..., a_{n - 1}~ đại diện cho ~n~ các giá trị của mảng.
Output
  • Một số nguyên duy nhất là kết quả của bài toán.
Constraints
  • ~1 \le n \le 10^4~
  • ~1 \le a_i \le 10^4~
Input Sample 1
5
1 2 3 4 5
Output Sample 1
-1
Input Sample 2
1
5
Output Sample 2
5

Comments

Please read the guidelines before commenting.


There are no comments at the moment.