p5019
//贪心 p5019
在这里插入代码片
//差分
#include<iostream>
using namespace std;
long long n,d[100005],ans,cf[100005]={0},sum1,sum2;
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>d[i];
for(int i=1;i<=n;i++)
{
cf[i]=d[i]-d[i-1];
if(cf[i]<0) sum1-=cf[i];
if(cf[i]>0) sum2+=cf[i];
}
cout<<max(sum1,sum2)<<endl;
return 0;
}
//分治
#include<iostream>
#include<cstdio>
using namespace std;
int a[10001],minn=0x7fffffff,n,des;
unsigned long long int sum;
void operate(int head,int tail)
{
minn=0x7fffffff;
for(int i=head;i<=tail;++i)
{
if(a[i]<minn)
{
minn=a[i];
des=i;
}
}
for(int i=head;i<=tail;++i)
{
a[i]-=minn;
}
sum+=minn;
return;
}
void search(int head,int tail)
{
if(tail<head) return;
operate(head,tail);
search(des+1,tail);
search(head,des-1);
return;
}
int main()
{
cin>>n;
for(int i=1;i<=n;++i)
{
cin>>a[i];
if(a[i]<minn)
{
minn=a[i];
}
}
search(1,n);
cout<<sum;
return 0;
}
// 贪心
#include<bits/stdc++.h>
using namespace std;
int n,a[100005];
long long ans=0;
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=2;i<=n;i++) if(a[i]>a[i-1]) ans+=a[i]-a[i-1];
cout<<ans+a[1];
return 0;
}