Problem:
Score : 800 points

Problem Statement

Given is a string S consisting of 0 and 1. Find the number of strings, modulo 998244353, that can result from applying the following operation on S between 0 and K times (inclusive):

Choose a pair of integers i, j (1\leq i < j\leq |S|) such that the i-th and j-th characters of S are 0 and 1, respectively. Remove the j-th character from S and insert it to the immediate left of the i-th character.

Constraints

1 \leq |S| \leq 300

0 \leq K \leq 10^9

S consists of 0 and 1.

Input

Input is given from Standard Input in the following format:

S K

Output

Find the number of strings, modulo 998244353, that can result from applying the operation on S between 0 and K times (inclusive).

Sample Input 1

0101 1

Sample Output 1

4

Four strings, 0101, 0110, 1001, and 1010, can result.

Sample Input 2

01100110 2

Sample Output 2

14

Sample Input 3

1101010010101101110111100011011111011000111101110101010010101010101 20

Sample Output 3

113434815

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;

public class CSharp{
    public static void Main(){
        Kyopuro chokudai = new Kyopuro();
	    chokudai.AtCoder();
	}
}

public class Kyopuro{

	string s;
	int k;
	
    public void AtCoder(){
		int p = 998244353;
        string[] input;
		input = Console.ReadLine().Split(' ');
		s = input[0];
		k = int.Parse(input[1]);
		
		int l = s.Length;
		List<int> a = new List<int>();
		int c = 0;
		a.Add(0);
		for (int i=l-1; i>-1; i--) {
			if (s[i] == '0') {
				a.Add(c);
				c = 0;
			} else {
				c++;
			}
		}
		int n = a.Count;
		a.Add(c);
		int m = 302;
		if (m > k) m = k;
		m++;

		long[][][] dp = new long[n+1][][];
		long[][][] cum2 = new long[n+1][][];
		long[][][] cum3 = new long[n+1][][];
		for (int i=0; i<=n; i++) {
			dp[i] = new long[m][];
			cum2[i] = new long[m][];
			cum3[i] = new long[m][];
			for (int j=0; j<m; j++) {
				dp[i][j] = new long[m];
				cum2[i][j] = new long[m];
				cum3[i][j] = new long[m];
			}
		}
		dp[0][0][0] = 1;
		cum2[0][0][0] = 1;
		cum3[0][0][0] = 1;

		for (int i=1; i<=n; i++) {
			for (int j=0; j<m; j++) {
				cum2[i-1][0][j] = dp[i-1][0][j];
				cum3[i-1][j][0] = dp[i-1][j][0];
				for (int h=1; h<m; h++) {
					cum2[i-1][h][j] = cum2[i-1][h-1][j] + dp[i-1][h][j];
					cum3[i-1][j][h] = cum3[i-1][j][h-1] + dp[i-1][j][h];
					cum2[i-1][h][j] %= p;
					cum3[i-1][j][h] %= p;
				}
			}
			for (int j=0; j<m; j++) {
				for (int h=0; h<m; h++) {
					if (h > j) continue;
					dp[i][j][h] += cum2[i-1][j][h];
					if (j - a[i] > 0) {
						dp[i][j][h] += p;
						dp[i][j][h] -= cum2[i-1][j-a[i]-1][h];
					}
					//dp[i][j][h] += cum3[i-1][j][j];
					if (h > 0) {
						//dp[i][j][h] += p;
						//dp[i][j][h] -= cum3[i-1][j][h-1];
						dp[i][j][h] += cum3[i-1][j][h-1];
					}
					dp[i][j][h] %= p;
				}
			}
		}

		long ans = 0;
		for (int i=0; i<m; i++) {
			ans += dp[n][i][i];
			ans %= p;
		}
		
		Console.WriteLine(ans);
    }
    public static void DebugWL(object x){ System.Diagnostics.Debug.WriteLine(x); }
}