For each $i$, find minimal $j>i$ such that $A[j]>A[i]$
its next greater element problem
its solvable using stack 
u can learn it from here (in method 2)
for more practice of this useful method u can solve these problems:

Largest Rectangle
https://www.hackerrank.com/challenges/largest-rectangle/

And xor Or
https://www.hackerrank.com/challenges/and-xor-or

Poisonous Plants
https://www.hackerrank.com/challenges/poisonous-plants

196A - Codeforces
http://codeforces.com/problemset/problem/196/A

319B - Codeforces
http://codeforces.com/problemset/problem/319/B

Minimum Sum
http://agc005.contest.atcoder.jp/tasks/agc005_b

these are solvable using #next_greater_element