Given a toxicity value for each of 2^L bitmasks, answer Q queries: each query fixes some bits and leaves others free, and asks the sum of values over all matching masks.
Medium7Bit manipulationPrefix sumDynamic programmingMathNo attempts yetTime limit2sMemory limit64 MBJOI Laboratory keeps 2L poisonous snakes, numbered 0,1,…,2L−1. Each snake is divided into L parts from the head to the tail, and each part is either blue or red. Write the number of snake i in binary as i=∑k=1Lck2L−k (0≤ck≤1). Then
Each snake has an integer between 0 and 9, inclusive, called its toxicity. You are given a string S of length 2L consisting of the digits 0 to 9. The i-th character of S (1≤i≤2L) is the toxicity of snake i−1.
Snakes are quick, so they often escape from JOI Laboratory. People living near the laboratory file complaints when they see snakes escaping.
You are given the complaints for Q days. The complaint for the d-th day (1≤d≤Q) is a string Td of length L consisting of the characters 0, 1, and ?.
Every complaint is accurate. All snakes that escaped were caught by the laboratory staff on the same day, so the same snake may escape again on a different day.
To estimate the risk of escaping snakes, Professor K, the executive director of JOI Laboratory, wants to know, for each day, the sum of the toxicities of the snakes that might have escaped. That is, for the d-th day you must add up the toxicities of all snakes that do not contradict Td.
Given the string S describing the toxicities and the complaints for Q days, write a program that computes, for each day, the sum of the toxicities of the snakes that might have escaped from the laboratory.
Note that the memory limit for this task is small.
Read the following data from standard input.
Write Q lines to standard output. The d-th line must contain one integer: the sum of the toxicities of the snakes that might have escaped on the d-th day.