Competitive Programming Solutions

GitHub
back to dashboard

A. Destroying Towers

Codeforces - Order Capital Round 2 (Codeforces Round 1104, Div. 1 + Div. 2) · limit: 1s · memory: 256MB · view on codeforces

input

10
3
1 3 5
3
5 4 3
4
3 2 5 1
4
2 1 4 3
5
4 1 3 5 2
5
2 2 3 1 4
1
7
6
6 1 5 2 4 3
4
1 1 1 1
5
10 3 8 6 9

output

3
12
8
5
8
8
7
11
4
22
Main.java
static void solve() {

        // Write your solution here
        int n = in.nextInt();
        int[] arr = readIntArray(n);
        int sum = arr[0];
        for(int i = 1; i < n; i++){
            if(arr[i] > arr[i - 1]){
                arr[i] = arr[i - 1];
                
            }
            sum += arr[i];
        }
        out.println(sum);

    }