#include <iostream> using namespace std; int n; int a[128],b[128]; int main() { scanf("%d",&n); for(int i=0;i<n;++i) scanf("%d",&a[i]); b[0]=a[0]; for(int i=1;i<n-1;++i){ if(a[i]>b[i-1]&&a[i]>a[i+1]) b[i]=max(b[i-1],a[i+1]); else if(a[i]<b[i-1]&&a[i]<a[i+1]) b[i]=min(b[i-1],a[i+1]); else b[i]=a[i]; } b[n-1]=a[n-1]; int ans=0; for(int i=0;i<n;++i){ ans+=abs(a[i]-b[i]); if(i) ans+=abs(b[i]-b[i-1]); } printf("%d",ans); return 0; }
原文地址:http://blog.csdn.net/sepnine/article/details/46125691