CF 1400E Clear the Multiset (贪心+分治)

tech2023-06-21  104

You have a multiset containing several integers. Initially, it contains a1a1 elements equal to 11, a2a2 elements equal to 22, ..., anan elements equal to nn.

You may apply two types of operations:

choose two integers ll and rr (l≤rl≤r), then remove one occurrence of ll, one occurrence of l+1l+1, ..., one occurrence of rr from the multiset. This operation can be applied only if each number from ll to rr occurs at least once in the multiset;choose two integers ii and xx (x≥1x≥1), then remove xx occurrences of ii from the multiset. This operation can be applied only if the multiset contains at least xx occurrences of ii.

What is the minimum number of operations required to delete all elements from the multiset?

Input

The first line contains one integer nn (1≤n≤50001≤n≤5000).

The second line contains nn integers a1a1, a2a2, ..., anan (0≤ai≤1090≤ai≤109).

Output

Print one integer — the minimum number of operations required to delete all elements from the multiset.

Examples

input

Copy

4 1 4 1 1

output

Copy

2

input

Copy

5 1 0 1 0 1

output

Copy

3

题解:操作1是将一个连续不含0的区间减1,操作2是将任意一个数清零。容易想到,对于操作1,如果不能把区间中的某个数减为0,相当于这些操作都白给。因此,对于某个区间,我们将整个区间减去它的最小值,再对不含0的子区间做相同的操作,统计的时候和区间长度比较取最小即可。

#include<iostream> #include<cstdlib> #include<cstdio> #include<algorithm> #include<cmath> #include<string.h> #include<string> using namespace std; const int INF=1000000001,maxn=5002; int a[maxn]; int get(int l,int r) { if (l>r) return 0; if (l==r) { return min(a[l],1); } int x=INF,sum=0; for (int i=l;i<=r;i++) if (x>a[i]) x=a[i]; for (int i=l;i<=r;i++) a[i]-=x; int pl=l-1; for (int i=l;i<=r;i++) { if (a[i]==0) { sum+=get(pl+1,i-1); pl=i; } } if (pl!=r) sum+=get(pl+1,r); return min(sum+x,r-l+1); return min(get(l,x-1)+get(x+1,r),r-l+1); } int main() { int n; cin>>n; for (int i=1;i<=n;i++) cin>>a[i]; int l=0,ans=INF; ans=min(get(1,n),n); cout<<ans; }

 

最新回复(0)