Codeforces Round #162 (Div. 1) C Choosing Balls DP

Source: Internet
Author: User

Dp[i] Indicates the maximum value ending with a color of I
Dp[i] = max (Dp[i], dp[i] + a*v[i], Other_max + b*v[i]);
The maximum value for a color other than the color I
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace Std;
const int MAXN = 100010;
Const __int64 inf = 0X7FFFFFFFFFFFFFFF;
__int64 DP[MAXN];
__int64 C[MAXN];
__int64 V[MAXN];
int main ()
{
Freopen ("Input.txt", "R", stdin);
int n, q;
while (~SCANF ("%d%d", &n, &q))
{
for (int i = 1;i <= n;i++)
scanf ("%i64d", &v[i]);
for (int i = 1;i <= n;i++)
scanf ("%i64d", &c[i]);
while (q--)
{
for (int i = 1;i <= n;i++)
Dp[i] =-inf;
__int64 A, B;
scanf ("%i64d%i64d", &a, &b);
__int64 ma = 0, ma_s = 0, ma_c = 0;
for (int i = 1;i <= n;i++)
{
__int64 temp_b;
__int64 temp_a;
if (dp[c[i]] = =-inf)
temp_a = B*v[i];
Else
Temp_a = A*v[i] + dp[c[i];
if (Ma_c = = C[i])
{
Temp_b = B*v[i] + ma_s;
Dp[c[i]] = MAX (max (Dp[c[i], temp_b), temp_a);
MA = Max (MA, Dp[c[i]]);
}
Else
{
Temp_b = b*v[i] + ma;
Dp[c[i]] = MAX (max (Dp[c[i], temp_b), temp_a);
if (Dp[c[i]] >= MA)
{
ma_s = ma;
Ma_c = C[i];
Ma = dp[c[i]];
}
else ma_s = max (ma_s, Dp[c[i]]);
}
}
printf ("%i64d\n", MA);
}
}
return 0;
}





























































Codeforces Round #162 (Div. 1) C Choosing Balls DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.