1787C - Remove the Bracket - CodeForces Solution


dp math

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define ls p<<1
#define rs p<<1|1
#define Ma 1000005
#define mod 1000000007
#define PLL pair<ll,ll>
#define PDD pair<double,double>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fi first
#define se second
#define N 61
#define pb push_back
#define ld long double
#define inf 1e18
using namespace std;
ll n,s;
ll a[Ma];
ll x[Ma],y[Ma];
ll dp[Ma][2];

void sol()
{
	scanf("%lld%lld",&n,&s);
	for (ll i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	for (ll i=2;i<n;i++)
	{
		x[i]=min(s,a[i]),y[i]=a[i]-x[i];
		if (x[i]>y[i])
			swap(x[i],y[i]);
	}
	dp[2][0]=x[2]*a[1],dp[2][1]=y[2]*a[1];
	for (ll i=3;i<n;i++)
	{
		dp[i][0]=min(dp[i-1][0]+y[i-1]*x[i],dp[i-1][1]+x[i-1]*x[i]);
		dp[i][1]=min(dp[i-1][0]+y[i-1]*y[i],dp[i-1][1]+x[i-1]*y[i]);
	}	
	printf("%lld\n",min(dp[n-1][0]+y[n-1]*a[n],dp[n-1][1]+x[n-1]*a[n]));
	return;
}

int main()
{
	ll tt;
	scanf("%lld",&tt);
	while (tt--)
		sol();
	return 0;
}


Comments

Submit
0 Comments
More Questions

431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick
18C - Stripe
1203B - Equal Rectangles
1536A - Omkar and Bad Story
1509A - Average Height
1506C - Double-ended Strings
340A - The Wall
377A - Maze
500A - New Year Transportation