Educational Codeforces Round 108 (Rated for Div. 2) D. Maximum Sum of Products

34 篇文章 1 订阅

传送门
You are given two integer arrays a and b of length n.

You can reverse at most one subarray (continuous subsegment) of the array a.

Your task is to reverse such a subarray that the sum ∑i=1nai⋅bi is maximized.

Input

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

The second line contains n integers a1,a2,…,an (1≤ai≤107).

The third line contains n integers b1,b2,…,bn (1≤bi≤107).

Output

Print single integer — maximum possible sum after reversing at most one subarray (continuous subsegment) of a.

思路:

dp(x,y):表示翻转x,y区间所获得的额外价值,则区间dp过程中记录翻转所得到的最大价值,最后再加上原有的价值。

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
ll a[5010];
ll b[5010];
ll dp[5010][5010];

int main()
{
	int n;
	cin>>n;
	ll ans = 0;
	ll sum = 0;
	for(int i = 1; i <= n; i++)cin>>a[i];
	for(int i = 1; i <= n; i++)cin>>b[i];
	for(int i = 1; i <= n; i++)sum += a[i]*b[i];
	for(int i = n-1; i >= 1; i--)
	{
		for(int j = i+1; j <= n; j++)
		{
			dp[i][j] = dp[i+1][j-1] + a[i]*b[j] + a[j]*b[i] - a[i]*b[i] - a[j]*b[j]; 
			ans = max(ans,dp[i][j]);
		}
	}
	printf("%lld\n",ans+sum);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值