Codeforce1176F Destroy it!

Description

You are playing a computer card game called Splay the Sire. Currently you are struggling to defeat the final boss of the game.spa

The boss battle consists of n turns. During each turn, you will get several cards. Each card has two parameters: its cost ci and damage di. You may play some of your cards during each turn in some sequence (you choose the cards and the exact order they are played), as long as the total cost of the cards you play during the turn does not exceed 3. After playing some (possibly zero) cards, you end your turn, and all cards you didn't play are discarded. Note that you can use each card at most once.code

Your character has also found an artifact that boosts the damage of some of your actions: every 10-th card you play deals double damage.ip

What is the maximum possible damage you can deal during n turns?ci

Input

The first line contains one integer n (1≤n≤2⋅\(10^{5}\)) — the number of turns.get

Then n blocks of input follow, the i-th block representing the cards you get during the i-th turn.input

Each block begins with a line containing one integer ki (1≤ki≤2⋅\(10^{5}\)) — the number of cards you get during i-th turn. Then ki lines follow, each containing two integers cj and dj (1≤cj≤3, 1≤dj≤\(10^{9}\)) — the parameters of the corresponding card.string

It is guaranteed that \(\sum_{i=1}^{n}\)ki≤2⋅\(10^{5}\).it

Output

Print one integer — the maximum damage you may deal.io

Solution

就是一个01背包加入了一些特殊的条件:
1.将这些物品分红n块,每块的质量之和不能超过3
2.当选到第10的倍数个数时,该数贡献翻倍
设f[i][j]表示作到第i个数,选了j个数的最大价值和
显然\(n^{2}\)咱们没法接受,因而咱们把j滚动
其余和01背包是同样的class

Code

#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 200001
#define open(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
using namespace std;
int n,i,j,k,a,b,t,ma,mi,m1[N][10],m2[N],m3[N];
long long ans,f[N][10];
int main()
{
    open("destroy");
    scanf("%d",&n);
    for (i=1;i<=n;i++)
    {
        scanf("%d",&t);
        for (j=1;j<=t;j++)
        {
            scanf("%d%d",&a,&b);
            if (a==1)
            {
                if (b>m1[i][1]) m1[i][3]=m1[i][2],m1[i][2]=m1[i][1],m1[i][1]=b;else 
                if (b>m1[i][2]) m1[i][3]=m1[i][2],m1[i][2]=b;else 
                if (b>m1[i][3]) m1[i][3]=b;
            }
            if (a==2) m2[i]=max(m2[i],b);
            if (a==3) m3[i]=max(m3[i],b);
        }
    }
    memset(f,128,sizeof(f));
    f[0][0]=0;
    for (i=1;i<=n;i++)
    {
        for (j=0;j<=9;j++) f[i][j]=f[i-1][j];
        for (k=0;k<=9;k++)
        {
            if (f[i-1][k]>=0) 
            {
                if (m1[i][1])f[i][(k+1)%10]=max(f[i][(k+1)%10],f[i-1][k]+m1[i][1]*(k==9?2:1));
                if (m1[i][1]&&m1[i][2]) f[i][(k+2)%10]=max(f[i][(k+2)%10],f[i-1][k]+m1[i][1]*(k>=8?2:1)+m1[i][2]);
                if (m2[i]) f[i][(k+1)%10]=max(f[i][(k+1)%10],f[i-1][k]+m2[i]*(k==9?2:1));
                if (m1[i][1]&&m1[i][2]&&m1[i][3]) f[i][(k+3)%10]=max(f[i][(k+3)%10],f[i-1][k]+m1[i][1]*(k>=7?2:1)+m1[i][2]+m1[i][3]);
                if (m1[i][1]&&m2[i]) 
                {
                    ma=max(m1[i][1],m2[i]);mi=min(m1[i][1],m2[i]);
                    f[i][(k+2)%10]=max(f[i][(k+2)%10],f[i-1][k]+ma*(k>=8?2:1)+mi);
                }
                if (m3[i]) f[i][(k+1)%10]=max(f[i][(k+1)%10],f[i-1][k]+m3[i]*(k==9?2:1));
            }
        }
    }
    for (i=0;i<=9;i++) ans=max(ans,f[n][i]);
    printf("%lld",ans);
    return 0;
}