Simple Sum

4.3

7 votes
Advanced Data Structures, Data Structures, Medium, Range query, Segment Trees
Problem

You have been given an array of N integers A1,A2..AN. You have to find simple sum for this array. Simple Sum is defined as i=Ni=1j=Nj=imax(Ai,Ai+1,,Aj)(Ai|Aj). | denotes the bitwise OR operator.

INPUT:
First line of input consists of integer N. Next line of input consists of N integers A1,A2..AN.

OUTPUT:
Print the simple sum of given array.

CONSTRAINTS:
1 ≤ N ≤ 105
1 ≤ Ai ≤ 104

Sample Input
5
4 2 3 3 3 
Sample Output
209
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

If you apply the given formula for the the array, you will get answer as 209.

Editor Image

?