SCU 4441: Necklace(dp+树状数组)

Necklace

frog has nn gems arranged in a cycle, whose beautifulness are a1,a2,,ana1,a2,…,an. She would like to remove some gems to make them into a beautiful necklacewithout changing their relative order.

Note that a beautiful necklace can be divided into 33 consecutive parts X,y,ZX,y,Z, where

  1. XX consists of gems with non-decreasing beautifulness,
  2. yy is the only perfect gem. (A perfect gem is a gem whose beautifulness equals to 1000010000)
  3. ZZ consists of gems with non-increasing beautifulness.

Find out the maximum total beautifulness of the remaining gems.

Input

The input consists of multiple tests. For each test:

The first line contains 11 integer nn (1n1051≤n≤105). The second line contains nn integers a1,a2,,ana1,a2,…,an. (0ai1040≤ai≤1041number of perfect gems101≤number of perfect gems≤10).

Output

For each test, write 11 integer which denotes the maximum total remaining beautifulness.

Sample Input

    6
    10000 3 2 4 2 3
    2
    10000 10000

Sample Output

    10010

10000

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
int a[3*maxn],n,ma,bit[10002],dp[maxn*3];
int query(int i)
{
    int s=0;
    while(i>0)
    {
        s=max(s,bit[i]);
        i-=i&-i;
    }
    return s;
}
void add(int i,int x)
{
    while(i<=10000)
    {
        bit[i]=max(bit[i],x);
        i+=i&-i;
    }
}
void slove(int x)
{
    memset(dp,0,sizeof(dp));
    memset(bit,0,sizeof(bit));
    for(int i=x+1,j=1;j<n;j++,i++)
    {
        if(a[i]==10000)continue;
        int tmp=query(10000-a[i])+a[i];
        add(10000-a[i],tmp);
        dp[i]=max(tmp,dp[i-1]);
    }
    memset(bit,0,sizeof(bit));
    for(int i=x-1,j=1;j<n;j++,i--)
    {
        if(a[i]==10000)continue;
        int tmp=query(10000-a[i])+a[i];
        add(10000-a[i],tmp);
        dp[i]=max(tmp,dp[i+1]);
    }
    for(int i=1;i<n;i++)
        ma=max(ma,dp[x-i]+dp[x+n-i-1]);
}
int main()
{
    while(~scanf("%d",&n))
    {
        ma=0;
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)a[i+n]=a[i+2*n]=a[i];
        for(int i=n+1;i<=2*n;i++)
        {
            if(a[i]==10000)
            {
                slove(i);
            }
        }
        printf("%d\n",ma+10000);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值