Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk).
Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost.
Input
The first line contains an integer n (1 ≤ n ≤ 200000). The second line contains n integers a1, a2, …, an, where ai is 0 if the cow number i is facing left, and 1 if it is facing right.
Output
Print a single integer, the minimum amount of lost milk.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
inputCopy
4
0 0 1 0
outputCopy
1
inputCopy
5
1 0 1 0 1
outputCopy
3
Note
In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
题意:挤牛奶,但是那些可以看见被挤牛奶的奶牛的奶牛会损失一个单位的牛奶,且可以多次损失知道它们自己也被挤了牛奶,求最小损失量;
考虑贪心:很容易发现:当最后的排列为00000……00011111…11111时,是不损失牛奶的,从左向右遍历一遍,标号为1 的就加上其右边为0 奶牛的数量,直到遍历完全;注意:时限1000ms,所以用前缀和维护一下就好了,总复杂度 O( n ).
#include
#include
#include
#include
#include
#include
#include
#include
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net