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