[POI2008]BBB-BBB

题目描述

Byteasar has an account at The Byteotian Bit Bank (BBB in short). At the beginning there were ![](http://main.edu.pl/images/OI15/bbb-en-tex.1.png) and at the end ![](http://main.edu.pl/images/OI15/bbb-en-tex.2.png) bythalers in the account. Each transaction was either a deposit or a withdrawal of one bythaler. The account's balance was never negative. A bank teller has prepared a bank statement: a strip of paper with a sequence of pluses and minuses in it (a plus denotes a deposit while minus a withdrawal of one bythaler). Soon it turned out, that some transactions were not entered correctly. The bank teller cannot print another statement, but has to correct the one already printed instead. The statement needs not be consistent with the truth, it will suffice if the sequence of transactions satisfies the following two conditions: the final balance is consistent with the initial balance and the sequence of transactions in the statement, according to the sequence of transactions in the statement, the account's balance was never negative. You are to calculate the minimum amount of time the bank teller needs to correct the bank statement. The bank teller can: in ![](http://main.edu.pl/images/OI15/bbb-en-tex.3.png) seconds turn an arbitrarily chosen transaction to its opposite, or in ![](http://main.edu.pl/images/OI15/bbb-en-tex.4.png) seconds remove the last transaction and put it at the beginning of the statement. If, for example, ![](http://main.edu.pl/images/OI15/bbb-en-tex.5.png), then --++-+-++-+-+ is a correct statement. On the other hand the statement ---++++++ is incorrect, because the account's balance would become negative after the third transaction, and furthermore the final balance should be 3, not 5. It can be, however, corrected by turning the second to last symbol to its opposite and placing the last transaction at the beginning of the statement. Task Write a programme that: reads the current bank statement for Byteasar's account (a sequence of pluses and minuses) as well as the numbers ![](http://main.edu.pl/images/OI15/bbb-en-tex.6.png), ![](http://main.edu.pl/images/OI15/bbb-en-tex.7.png), ![](http://main.edu.pl/images/OI15/bbb-en-tex.8.png) and ![](http://main.edu.pl/images/OI15/bbb-en-tex.9.png) from the standard input. writes out to the standard output the minimum number of seconds needed to correct the statement in a way such that the initial and final balance are consistent and that the balance is never negative. 给定一个由+1和−1构成的长度为n的序列,提供两种操作: 1.将某一位取反,花销为x 2.将最后一位移动到第一位,花销为y 要求最终p+sumn=q,且p+sumi≥0(1≤i≤n),求最小花销 sumi为1-i的前缀和

输入输出格式

输入格式


The first line contains 5 integers ![](http://main.edu.pl/images/OI15/bbb-en-tex.10.png), ![](http://main.edu.pl/images/OI15/bbb-en-tex.11.png), ![](http://main.edu.pl/images/OI15/bbb-en-tex.12.png), ![](http://main.edu.pl/images/OI15/bbb-en-tex.13.png) and ![](http://main.edu.pl/images/OI15/bbb-en-tex.14.png) (![](http://main.edu.pl/images/OI15/bbb-en-tex.15.png), ![](http://main.edu.pl/images/OI15/bbb-en-tex.16.png), ![](http://main.edu.pl/images/OI15/bbb-en-tex.17.png)), separated by single spaces and denoting respectively: the number of transactions done by Byteasar, initial and final account balance and the number of seconds needed to perform a single turn (change of sign) and move of transaction to the beginning. The second line contains a sequence of ![](http://main.edu.pl/images/OI15/bbb-en-tex.18.png) signs (each a plus or a minus), with no spaces in-between.

输出格式


The first and last output line should contain one integer, the minimum number of seconds needed to correct the statement. If no corrections are necessary, the number is zero. You may assume that a proper sequence of modifications exists for each test data.

输入输出样例

输入样例 #1

9 2 3 2 1
---++++++

输出样例 #1

3