• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Codeforce Round #225 Div2 C

   
     
 

            
Sponsored by             
In English             По-русски        
HaibaraAi                 |                  Logout                        
                                     You have                      Wow! You have +182!                             
 
 
 
 
  • Home
  • Contests
  • Gym
  • Problemset
  • Groups
  • Rating
  • Help
                                     
        
 
 
Codeforces Round #225 (Div. 2)
Finished
Contestant
Add to favourites         
 
 
→ Practice?            
 
        Want to solve the contest problems after the official contest ends? Just register for practice and you will be able to submit solutions.    
                      
 
 
→ Last submissions            
 
SubmissionTimeVerdict
5753504 01/20/2014 08:27PM Wrong answer on pretest 3
5753451 01/20/2014 08:26PM Wrong answer on pretest 3
 
 
→ Problem tags            
 
 
 
 
 
    greedy     
No tag edit access    
              
              
 
 
→ Contest materials
 
  • Announcement
     
                
  •  
     
     
  • Problems
  • Submit
  • My submissions
  • Status
  • Hacks
  • Room
  • Standings
  • Custom test
C. Milking cows
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
Input
4 0 0 1 0
Output
1
Input
5 1 0 1 0 1
Output
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.

            

Codeforces (c) Copyright 2010-2014 Mike Mirzayanov
The only programming contests Web 2.0 platform
Server time: 01/21/2014 01:34PM (p1).
 
 
 

 

 1 #pragma comment(linker,"/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <vector>
 4 #include <cmath>
 5 #include <queue>
 6 #include <cstring>
 7 #include <iostream>
 8 #include <algorithm>
 9 using namespace std;
10 #define INF 0x7fffffff
11 #define mod 1000000007
12 #define ll long long
13 #define maxn 200025
14 int n, m, k;
15 int a[maxn];
16 int main(){
17     scanf("%d", &n);
18     ll t = 0, x = 0;
19     for (int i = 0; i < n; i++){
20         scanf("%d", &a[i]);
21         if (a[i] == 1)t++;
22         if (a[i] == 0)x += t;
23     }
24     ll ans = 10000000000000000;
25     ans = min(ans, x);
26     x = 0, t = 0;
27     for (int i = n - 1; i >= 0; i--){
28         if (a[i] == 0)t++;
29         if (a[i] == 1)x += t;
30     }
31     ans = min(ans, x);
32     printf("%I64d\n", ans);
33     return 0;
34 }
View Code
posted @ 2014-01-21 17:37  HaibaraAi  阅读(95)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3