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. svn: Checksum mismatch while updating 错误

    最近使用svn客户端更新代码的时候出现 Checksum mismatch while updating 的错误 解决办法 在出错文件的目录下,用update to reversion , 先选onl ...

  2. 超酷算法-BK树

    前几天无意间遇到一个博客,觉得写得挺好的,自己之前的时候有个不好的习惯,那就是遇到了好资源第一反应就是收藏起来然后却很少再看!!这是坏习惯,要改!于是今天就开始通读了,读的第二篇是BK树.觉得有点意思 ...

  3. Java 异常(Java Exception)

    Java异常    异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等.异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程.Java通 过API中Throwable类的众多子类 ...

  4. ZOJ3874 Permutation Graph

    Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward has a permutation {a1, a2, … an}. He finds ...

  5. 【BZOJ】2196: [Usaco2011 Mar]Brownie Slicing

    [题意]给定n*m的数字矩阵,要求横着切A-1刀,对每块再分别竖着切B-1刀,是最小子矩阵最大. [算法]二分+贪心 [题解]还记得提高组2015跳石头吗?这道题做法一致,只不过拓展到二维而已. 二分 ...

  6. 【BZOJ】1609: [Usaco2008 Feb]Eating Together麻烦的聚餐

    [算法]动态规划 [题解]DP有个特点(递推的特点),就是记录所有可能状态然后按顺序转移. 最优化问题中DP往往占据重要地位. f[i][j]表示前i头奶牛,第i头改为号码j的最小改动数字,这样每头奶 ...

  7. quick-cocos2d-x数据存储 UserDefault GameState io

    看了quick-cocos2d-x 的framework,发现里面有一个GameState,查了下,是数据存储的类,于是稍稍总结下我用到过的数据存储方式吧. 一共是三种方法: cc.UserDefau ...

  8. es6异步操作

    异步编程对 JavaScript 语言太重要.JavaScript 只有一根线程,如果没有异步编程,根本没法用,非卡死不可. ES6 诞生以前,异步编程的方法,大概有下面四种. 回调函数 事件监听 发 ...

  9. Html5_sessionStrong和localStorage的灵活使用

    谈谈这两个属性sessionStrong和localStorage是Html5新增点属性,用来记录一些数据在浏览器. 两者的区别sessionStrong存储的数据是暂时的,浏览器关掉后,存储下来的数 ...

  10. E题hdu 1425 sort

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 sort Time Limit: 6000/1000 MS (Java/Others)    M ...