#P8919. Minimize Message Withdrawals
Minimize Message Withdrawals
Minimize Message Withdrawals
Xiao A is playing a game in his group chat. The game works as follows: given a function \(f\) defined on positive integers, at some point the group starts assigning positions to messages. When a message is sent, its position (its rank in the current message order) is determined by the number of messages that remain in the chat. If the function \(f\) evaluates to \(1\) at that position, i.e. \(f(x)=1\), then the sender of that message will receive a small penalty.
The game ends when the total number of messages reaches \(m\). Xiao A, who tends to chat a lot, has sent \(n\) messages. His \(i\)th sent message originally occupies the position \(a_i\) in the overall message order.
Being an administrator, Xiao A can withdraw (i.e. delete) any message at any time (whether it is his or someone else’s). Note that withdrawing a message will cause the positions (ranks) of all subsequent messages to decrease by one. However, withdrawing too many messages might be seen as tyranny, so he wishes to withdraw as few messages as possible. In particular, he wants to ensure that after some messages are withdrawn, in the final message order none of his messages end up in a position \(x\) for which \(f(x)=1\).
You are given the number \(n\), the function \(f\) and the positions \(a_i\) of Xiao A’s messages in the original order. Help him determine the minimum number of messages that he needs to withdraw so that in the final ordering, every message sent by him is positioned at some \(x\) with \(f(x)=0\). (Other messages are not restricted.)
Note: The function \(f\) is provided as a binary string of length \(m\), where the \(x\)th character (1-indexed) represents \(f(x)\) (either 0 or 1).
inputFormat
The input consists of three lines:
- The first line contains two integers \(n\) and \(m\) separated by a space, where \(n\) is the number of messages sent by Xiao A, and \(m\) is the total number of messages originally in the chat.
- The second line contains a binary string of length \(m\) representing \(f\): the \(x\)th character (1-indexed) denotes \(f(x)\) (either 0 or 1).
- The third line contains \(n\) space-separated integers: \(a_1, a_2, \dots, a_n\) (in increasing order), where \(a_i\) is the original position of Xiao A's \(i\)th message.
outputFormat
Output a single integer – the minimum number of messages that must be withdrawn so that, in the final message order, every message originally sent by Xiao A appears at a position \(x\) with \(f(x)=0\).
sample
2 5
01000
2 4
1