148E - Porcelain - CodeForces Solution


dp *1900

Please click on ads to support us..

Python Code:

f = lambda: map(int, input().split())
n, m = f()
u = [0]
for i in range(n):
    t = list(f())
    k = t[0]
    for i in range(1, k): t[i + 1] += t[i]
    v = [t[k]] * (k + 1)
    v[0] = t[0] = 0
    for d in range(1, k):
        v[k - d] -= min(t[j + d] - t[j] for j in range(k - d + 1))
    p = [0] * (min(m, len(u) + len(v)) + 1)
    for i, x in enumerate(u):
        for j, y in enumerate(v, i):
            if j > m: break
            p[j] = max(p[j], x + y)
    u = p
print(u[m])

C++ Code:

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout.setf(ios::fixed), cout.precision(8);

    int n, m;
    cin >> n >> m;

    vector<vector<int>> a(n);
    for (int i = 0; i < n; i++) {
        int c;
        cin >> c;
        a[i].resize(c + 1);
        a[i].assign(c + 1, 1E5);
        a[i][0] = 0;
        vector<vector<int>> b(c + 1, vector<int> (c + 1));
        for (int j = 1; j <= c; j++) {
            int x;
            cin >> x;
            a[i][0] += x;
            for (int k = 1; k <= j; k++) {
                b[j][k] = x + b[j - 1][k - 1];
                a[i][c - k] = min(a[i][c - k], b[j][k]);
            }
        }
        for (int j = 1; j <= c; j++) {
            a[i][j] = a[i][0] - a[i][j];
        }
        a[i][c] = a[i][0];
        a[i][0] = c;
    } 

    vector<int> dp(m + 1);
    for (int i = 0; i < n; i++) {
        for (int j = m; j >= 0; j--) {
            for (int k = 1; k <= min(j, a[i][0]); k++) {
                dp[j] = max(dp[j], dp[j - k] + a[i][k]);
            }
        }
    }

    cout << dp[m];

    return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST