一个贪心算法,地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=2181

#include <stdio.h>  

int num[150005]; 

int main() 

    
    
int n;
    
int i, j; 
    
int sum;
    
while (scanf("%d"&n) != EOF)
    
{
        
for (i=0; i<n; i++
        
{
            scanf(
"%d"&num[i]);
        }

        sum 
= 0;
        
for(i=j=0; i<n-1; i++
        

            
if(j==0
            

                
if(num[i]>num[i+1]) 
                

                    sum 
+= num[i]; 
                    j 
= 1
                }
 
            }
 
            
else if(j==1
            

                
if(num[i]<num[i+1]) 
                

                    sum 
-= num[i]; 
                    j 
= 0
                }
 
            }
 
        }
 
        
if(j==0)
        
{
            sum 
+= num[n-1];
        }

        printf(
"%d\n", sum);
    }

    
return 0
}