solution:
Meaning of the questions we ask is to let each with a [i] at the end of the longest sub-sequence and did not fall.
Then we first pretreatment did not fall out of a length of the longest sequence at the end of every number, in E [i], i.e. to the i-th end.
Can be obtained with a base dp.
Then we define the sum array, it represents the longest i did not fall and the end of the subsequence.
Then the number of i enumerator before, are: When e [i] == e [j] +1 time, and a [i]> = a [j], i.e., when the end i can be, sum [i] = sum [j] + a [i], can directly out.
Final output just fine.
Code:
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #define inf 2147483647 using namespace std; int len=1; int n,a[20001]; int sum[20001]; int pre[20001]; int e[20001]; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=n;i++)sum[i]=a[i],e[i]=1; /*for(int i=2;i<=n;i++){ pre[1]=a[1]; for(int j=2;j<=i;j++){ if(a[j]>=pre[len])pre[++len]=a[j]; else{ int p=upper_bound(pre+1,pre+len+1,a[j])-pre; pre[p]=a[j]; } } e[i]=len; len=1; memset(pre,0,sizeof(pre)); }*/ for(int i=2;i<=n;i++) for(int j=1;j<i;j++) if(a[i]>=a[j])e[i]=max(e[i],e[j]+1); for(int i=1;i<=n;i++) for(int j=1;j<i;j++) if(e[j]+1==e[i]&&a[i]>=a[j]){ sum[i]=sum[j]+a[i]; break; } //for(int i=1;i<n;i++)if(sum[i+1]<sum[i])sum[i+1]=sum[i]; for(int i=1;i<=n;i++)printf("%d ",sum[i]); return 0; }
Note that I use the comment section above (nlogn) algorithm for the length of time out, however, note that the reciprocal determination and in the second cycle.