题解 AT5141 【[AGC035D] Add and Remove】
StudyingFather · · 题解
集训队作业 2019 AGC035D
考虑计算每个元素对答案的贡献次数。
我们来倒着考虑整个过程。刚开始我们只有两个元素,这两个元素对答案的贡献次数为
容易发现,设
我们设
转移的时候枚举断点,将整个区间拆分成两个区间递归求解即可。
转移方程为:
最后要求的当然是
代码写起来非常短(
#include <iostream>
#include <algorithm>
using namespace std;
long long a[25];
long long dfs(int l,int r,int xl,int xr)
{
if(r-l<=1)return 0;
long long ans=1e18;
for(int i=l+1;i<=r-1;i++)
ans=min(ans,dfs(l,i,xl,xl+xr)+dfs(i,r,xl+xr,xr)+a[i]*(xl+xr));
return ans;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
cout<<a[1]+a[n]+dfs(1,n,1,1)<<endl;
return 0;
}