第15屆浙江省賽 D Sequence Swapping(dp)

來源:互聯網
上載者:User

標籤:++   ber   increase   一個   multiple   def   output   name   red   

Sequence SwappingTime Limit: 1 Second      Memory Limit: 65536 KB

BaoBao has just found a strange sequence {<, >, <, >, , <, >} of length  in his pocket. As you can see, each element <, > in the sequence is an ordered pair, where the first element  in the pair is the left parenthesis ‘(‘ or the right parenthesis ‘)‘, and the second element  in the pair is an integer.

As BaoBao is bored, he decides to play with the sequence. At the beginning, BaoBao‘s score is set to 0. Each time BaoBao can select an integer , swap the -th element and the -th element in the sequence, and increase his score by , if and only if ,  ‘(‘ and  ‘)‘.

BaoBao is allowed to perform the swapping any number of times (including zero times). What‘s the maximum possible score BaoBao can get?

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains an integer  (), indicating the length of the sequence.

The second line contains a string  () consisting of ‘(‘ and ‘)‘. The -th character in the string indicates , of which the meaning is described above.

The third line contains  integers  (). Their meanings are described above.

It‘s guaranteed that the sum of  of all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the maximum possible score BaoBao can get.

Sample Input
46)())()1 3 5 -1 3 26)())()1 3 5 -100 3 23())1 -1 -13())-1 -1 -1
Sample Output
242102
Hint

For the first sample test case, the optimal strategy is to select  in order.

For the second sample test case, the optimal strategy is to select  in order.

題解:  一對括弧交換相當於左括弧向右移一位,右括弧想左移一位,  所有交換中所有左括弧的相對位置不變,右括弧同理。  可以從右邊開始枚舉每個左括弧移動的位置,左括弧移動  到一個點的加分等於此括弧到這個點的加分加上之前的括弧  所能到達的點的最大得分。  代碼:    #include<bits/stdc++.h>  using namespace std;  #define ll long long  const int maxn=1003;  char t[maxn];  ll a[maxn],dp[maxn][maxn];  int main()  {      int T;scanf("%d",&T);      while(T--)      {          memset(dp,0,sizeof(dp));          int n;scanf("%d",&n);          scanf("%s",t+1);          for(int i=1;i<=n;i++)scanf("%lld",&a[i]);          int now=0;          ll ma=0;          for(int i=n;i>=1;i--)          {              if(t[i]==‘(‘)              {                  now++;                  for(int j=i+1;j<=n;j++)                  {                      if(t[j]==‘(‘)dp[now][j]=dp[now][j-1];                      else dp[now][j]=a[i]*a[j]+dp[now][j-1];                      ma=max(dp[now][j]+dp[now-1][j],ma);                  }                  for(int j=i;j<=n;j++)dp[now][j]+=dp[now-1][j];                  for(int j=n;j>1;j--)dp[now][j-1]=max(dp[now][j-1],dp[now][j]);              }          }          printf("%lld\n",ma);      }      return 0;  }  

 

第15屆浙江省賽 D Sequence Swapping(dp)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.