Necklace

frog has \(n\) gems arranged in a cycle, whose beautifulness are \(a_1, a_2, \dots, a_n\). She would like to remove some gems to make them into a beautiful necklace without changing their relative order.

Note that a beautiful necklace can be divided into \(3\) consecutive parts \(X, y, Z\), where

  1. \(X\) consists of gems with non-decreasing beautifulness,
  2. \(y\) is the only perfect gem. (A perfect gem is a gem whose beautifulness equals to \(10000\))
  3. \(Z\) 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 \(1\) integer \(n\) (\(1 \leq n \leq 10^5\)). The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\). (\(0 \leq a_i \leq 10^4\), \(1 \leq \textrm{number of perfect gems} \leq 10\)).

Output

For each test, write \(1\) 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>
#define met(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long ll;
const int N = 1e5+;
int C[N];
int dp1[N],dp2[N],b[N],a[*N];
int n,m,k;
int low_bit(int x)
{
return x&(-x);
}
void updata(int pos,int val)
{
for(int i=pos;i<=;i+=low_bit(i))
C[i]=max(C[i],val);
}
int get_max(int pos)
{
int ans=;
for(int i=pos;i>;i-=low_bit(i))
ans=max(ans,C[i]);
return ans;
} int solve(int id)
{
met(C,);
int cnt=;
for(int i=id+;i<id+n;i++)
{
if(a[i]!=)
b[cnt++]=a[i];
}
dp1[]=b[];
updata(-b[],b[]);
for(int i=;i<cnt;i++)
{
dp1[i]=get_max(-b[i])+b[i];
updata(-b[i],dp1[i]);
}
met(C,);
dp2[cnt-]=b[cnt-];
updata(-b[cnt-],b[cnt-]);
for(int i=cnt-;i>=;i--)
{
dp2[i]=get_max(-b[i])+b[i];
updata(-b[i],dp2[i]);
}
int ans=;
for(int i=;i<cnt;i++)
dp1[i]=max(dp1[i],dp1[i-]);
for(int i=cnt-;i>;i--)
dp2[i]=max(dp2[i],dp2[i+]);
dp2[cnt]=;
for(int i=;i<cnt;i++)
ans=max(ans,dp1[i]+dp2[i+]);
ans=max(ans,dp2[]);
return ans+;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
a[i+n]=a[i];
}
int ans=;
for(int i=;i<n;i++)
{
if(a[i]==)
{
ans=max(ans,solve(i));
}
}
printf("%d\n",ans);
}
return ;
}
 

SCU - 4441 Necklace(树状数组求最长上升子数列)的更多相关文章

  1. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  2. POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8807   Accepted ...

  3. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  4. 树状数组求第k小的元素

    int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...

  5. POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)

    树状数组求逆序对   转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...

  6. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  7. poj 2985 The k-th Largest Group 树状数组求第K大

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8353   Accepted ...

  8. HDU 1394 Minimum Inversion Number (树状数组求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...

  9. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

随机推荐

  1. [洛谷P1941] 飞扬的小鸟

    洛谷题目链接:飞扬的小鸟 题目描述 Flappy Bird是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了 ...

  2. 转【es中数据节点和主机】

    在生产环境下,如果不修改elasticsearch节点的角色信息,在高数据量,高并发的场景下集群容易出现脑裂等问题. 默认情况下,elasticsearch集群中每个节点都有成为主节点的资格,也都存储 ...

  3. 【bzoj3376-方块游戏】带权并查集

    题意: n块积木,m个操作或询问.每次移动积木的时候,约翰会选择两块积木X,Y,把X搬到Y的上方.如果X已经和其它积木叠在一起了,那么应将这叠积木整体移动到Y的上方:如果Y已经和其它积木叠在一起了的, ...

  4. 【BZOJ4870】组合数问题 [矩阵乘法][DP]

    组合数问题 Time Limit: 10 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description Input 第一行有四个整数 n ...

  5. Achain 钱包部署

    官网 GIT: [ Achain_linux ] 基础环境 OS: CentOS, Ubuntu Achain: 官网 [ release 最新版本 ] 安装 Achain 钱包 下载 CentOS ...

  6. SSH ERROR: Too many Authentication Failures

    来自: How to recover from "Too many Authentication Failures for user root" 其中一种可以解决的方式 eval ...

  7. Optimal Milking(POJ2112+二分+Dinic)

    题目链接:http://poj.org/problem?id=2112 题目: 题意:有k台挤奶机,c头奶牛,每台挤奶机每天最多生产m的奶,给你每个物品到其他物品的距离(除了物品到自己本省的距离为0外 ...

  8. jQuery实现用户头像裁剪插件cropbox.js

    几乎每一个网页是必备图片上传,图片裁剪功能,这里通过cropbox.js插件实现该功能. <script src="js/jquery-1.11.1.min.js">& ...

  9. setTimeOut、setInterval与clearInterval函数

    1.setTimeOut 在指定毫秒数后调用函数或计算表达式,函数或计算表达式只执行一次 setTimeout("alert('5 seconds!')",5000) 2.setI ...

  10. bing查询旁站脚本

    #!/usr/bin/env python # -*- coding: UTF-8 -*- #by i3ekr import re,optparse,sys,requests,time,os pars ...