Educational Codeforces Round 164 D. Colored Balls

Colored Balls

time limit per test: 2 second
memory limit per test: 1024 megabytes
input: standard input
output: standard output

There are balls of n n n different colors; the number of balls of the i i i-th color is a i a_i ai.

The balls can be combined into groups. Each group should contain at most 2 2 2 balls, and no more than 1 1 1 ball of each color.

Consider all 2 n 2^n 2n sets of colors. For a set of colors, let’s denote its value as the minimum number of groups the balls of those colors can be distributed into. For example, if there are three colors with 3 3 3, 1 1 1 and 7 7 7 balls respectively, they can be combined into 7 7 7 groups (and not less than 7 7 7), so the value of that set of colors is 7 7 7.

Your task is to calculate the sum of values over all 2 n 2^n 2n possible sets of colors. Since the answer may be too large, print it modulo 998   244   353 998\,244\,353 998244353.

Input

The first line contains a single integer n n n ( 1 ≤ n ≤ 5000 1 \le n \le 5000 1n5000) — the number of colors.

The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 1 ≤ a i ≤ 5000 1 \le a_i \le 5000 1ai5000) — the number of balls of the i i i-th color.

Additional constraint on input: the total number of balls doesn’t exceed 5000 5000 5000.

Output

Print a single integer — the sum of values of all 2 n 2^n 2n sets of colors, taken modulo 998   244   353 998\,244\,353 998244353.

Example

i n p u t \tt input input
3
1 1 2
o u t p u t \tt output output
11
i n p u t \tt input input
1
5
o u t p u t \tt output output
5
i n p u t \tt input input
4
1 3 3 7
o u t p u t \tt output output
76

Note

Consider the first example. There are 8 8 8 sets of colors:

  • for the empty set, its value is 0 0 0;
  • for the set { 1 } \{1\} {1}, its value is 1 1 1;
  • for the set { 2 } \{2\} {2}, its value is 1 1 1;
  • for the set { 3 } \{3\} {3}, its value is 2 2 2;
  • for the set { 1 , 2 } \{1,2\} {1,2}, its value is 1 1 1;
  • for the set { 1 , 3 } \{1,3\} {1,3}, its value is 2 2 2;
  • for the set { 2 , 3 } \{2,3\} {2,3}, its value is 2 2 2;
  • for the set { 1 , 2 , 3 } \{1,2,3\} {1,2,3}, its value is 2 2 2.

So, the sum of values over all 2 n 2^n 2n sets of colors is 11 11 11.

Tutorial

设选中的球的总数为 m m m,其中数量最多的颜色的球有 x x x 个,则会出现以下两种情况:

  • 如果 x ≤ m − x x \leq m - x xmx,则至少分成 ⌈ m 2 ⌉ \lceil {m \over 2} \rceil 2m
  • 如果 x > m − x x > m - x x>mx,则至少分成 x x x​ 组

所以我们只需要计算 ∑ ( max ⁡ ( ⌈ m 2 ⌉ , x ) × 总和为  m  的方案数 ) \sum (\max(\lceil {m \over 2} \rceil,x) \times 总和为\ m\ 的方案数) (max(⌈2m,x)×总和为 m 的方案数)​ 即可

接下来可以用动态规划来解决这个问题,将数组从前往后遍历,在 i i i 位置,当前球的个数 a i a_i ai 就是当前数量最多的球的个数, d p j dp_j dpj 表示在此之前,其他球个数总和为 j j j 的方案数,则当前位置对答案的贡献就是 ∑ j = 0 c n t ( d p j × max ⁡ ( ⌈ j 2 ⌉ , x ) ) \sum_{j = 0}^{cnt}(dp_j \times \max(\lceil {j \over 2} \rceil,x)) j=0cnt(dpj×max(⌈2j,x)),其中 c n t = ∑ k = 0 i a k cnt= \sum_{k = 0}^i a_k cnt=k=0iak,然后直接按照背包问题进行计算即可

此解法时间复杂度为 O ( n 2 ) \mathcal O(n ^ 2) O(n2),即背包问题的时间复杂度

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long
const int mod = 998244353;

signed main() {
    int n, cnt = 0, ans = 0;
    cin >> n;
    vector<int> a(n);
    for (int &ai : a) {
        cin >> ai;
    }
    ranges::sort(a);
    vector<int> dp(5001);
    dp[0] = 1;
    for (int ai : a) {
        for (int i = cnt; ~i; --i) {
            if (dp[i]) {
                ans = (ans + dp[i] * max((i + ai + 1) / 2, ai)) % mod;
                dp[i + ai] = (dp[i + ai] + dp[i]) % mod;
            }
        }
        cnt += ai;
    }
    cout << ans << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值