F. Destroy it!
Topic Address: http: //codeforces.com/contest/1176/problem/F
Ideas: in fact, a 01 knapsack problem, just add the 01 rounds and each round of restrictions, and whenever the number of cards have been used to a multiple of 10, that card will trigger the double attack.
Because how many cards will trigger the use of double effect, so we have to record the number of recorded attacks while using the card, you can dp by the 01 backpack [N] [N] change,
The first dimension is the first several rounds of the current, the second dimension is the recording card with the attack caused by the case where the n, but dp [N] [N] (N <= 2e5),
Obviously not take up too much memory. I think of using dp [N] [10].
(Because the number of cards may be many, in fact, as long as we recorded a maximum of three 1 fee, a fee 2, a 3 fee)
each bout card case with a maximum of 3:
1 with a three card fee.
2. 2 a charge card or a charge card 1, a two rate card
3. 1 a charge card or a two card charges, charge card or a Joe Smith
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 #define rep(i,j,k) for(int i = (j); i <= (k); i++) 5 #define per(i,j,k) for(int i = (j); i >= (k); i--) 6 #define mod(x) ((x)%(MOD)) 7 8 typedef long long LL; 9 const int MOD = 10; 10 const int N = 2e5 + 10; 11 LL dp[N + 2][10]; 12 13 void init(){ 14 rep(i, 0, N) rep(j, 0, 9) dp[i][j] = -1; 15 } 16 17 int main(){ 18 19 ios::sync_with_stdio(false); 20 cin.tie(0); 21 22 init(); 23 24 int n; 25 cin >> n; 26 27 dp[0][0] = 0;//Initialization 28 29 REP (O, . 1 , n-) { 30 int NUM; 31 is CIN >> NUM; 32 33 is int Ll = 0 ; 34 is int L2 of = 0 ; 35 int L3 = 0 ; 36 int C1 [ . 5 ] = { 0 }; // fee 1, v maximum recorded three 37 [ int C2 = 0 ; // fee 2 38 is int C3 = 0 ; // fee 3 39 40 int c, v; 41 rep(i, 1, num){ 42 cin >> c >> v; 43 44 if (c == 1){ 45 if (L1 == 3){ 46 int x = 1; 47 if (c1[x] > c1[2]) x = 2; 48 if (c1[x] > c1[3]) x = 3; 49 if (v > c1[x]) c1[x] = v; 50 } 51 else c1[++L1] = v; 52 } 53 else if (c == 2){ 54 L2 = 1; 55 if (v > c2) c2 = v; 56 } 57 else if (c == 3){ 58 L3 = 1; 59 if (v > c3) c3 = v; 60 } 61 } 62 63 sort(c1 + 1, c1 + 1 + 3 ); 64- 65 int max_v = max (c1 [ 3 ], max (c2, c3)); // a maximum of v 66 int _2_1 = c1 [ 3 ]; // maximum of two cards cost 67 int _2_2 C1 = [ 2 ]; 68 IF (C2> _2_2) _2_2 = C2; 69 IF (_2_2> _2_1) the swap (_2_2, _2_1); // two cards from a maximum fee intermediate v 2, and an v 2 selected intermediate maximum stay two 70 LL C1 _3 = [ 1 ] + C1 [ 2 ] + C1 [ . 3 ]; // with three charges 1 71 is 72 REP (I,0 , . 9 ) DP [O] [I] DP = [O - . 1 ] [I]; // first save state to the current round round, lower side to facilitate comparison of 73 // because these three states are additional results in the states, so are independent between them. 74 // MOD (X + I), (I + X> = 10) * value for determining whether to satisfy a number greater than or equal to a multiple of ten 75 REP (I, 0 , . 9 ) { 76 IF (DP [ O - . 1 ] [I] = -! . 1 ) { // the states exist 77 IF (Ll + L3 + L2 of> = . 1 ) { // with the card when a minimum, by a case 78 79 DP [O] [MOD (I + . 1)] = Max (DP [O] [MOD (I + . 1 )], 80 DP [O - . 1 ] [I] + max_v + (I + . 1 > = 10 ) * max_v); 81 82 } 83 IF (L2 of Ll +> = 2 ) { // . 1 charges the cost of the card 2 has at least two, with the two cases 84 85 DP [O] [MOD (I + 2 )] = max (DP [O] [MOD (I + 2 )], 86 DP [O - . 1 ] [I] + _2_1 _2_2 + + (I + 2 > = 10 ) * _2_1); 87 88 } 89 IF (Ll> = . 3 ) { // . 1 fee minimum number of three cards, the case with three 90 91 is DP [O] [MOD (I + . 3 )] = max (DP [O] [MOD (I + . 3 )], 92 DP [O - . 1 ] [I] + _3 + (I + . 3 > = 10 ) * C1 [ . 3 ]); 93 94 } 95 } 96 } 97 } 98 99 LL ANS = - 1 ; 100 101 //rep(i, 0, n){ 102 // rep(o, 0, 9) cout << dp[i][o] << " "; 103 // cout << endl; 104 //} 105 //cout << endl; 106 107 rep(o, 0, 9) ans = max(dp[n][o], ans); 108 109 cout << ans << endl; 110 111 return 0; 112 }