在线求解 交了好几遍超时
DESCRIPTION
Given an integer array, how will you find out the increasing subsequence which gives the largest sum. For example, 4, 2, 1, 5, 3 in this subsequence a few possible increasing subsequences are 1) 4 2) 2 3) 1 4) 5 5) 3 6) 4, 5 7) 2, 5 8) 2, 3 9) 1, 5 10) 1, 3 but 4, 5 gievs the maximum sum. Your task is how to find such the maximum sum in a given integer array.
INPUT
Multiply Cases, For each case: The first line contains N(0< N <=1000000), which is the number of the integer array. The second line contains N integer numbers not exceeding 2000000 by the absolute value.
OUTPUT
For each case, output only one integer, which is the maximum sum of increasing subsequences.
SAMPLE INPUT
5
4 2 1 5 3
SAMPLE OUTPUT
9
运行了好多遍超时 求解:
#include<iostream>
using namespace std;
int p1[1000000];
int main()
{
int n;
while(cin>>n)
{
for(int k=0;k<n;k++)
cin>>p1[k];
int temp;
for(int i=1;i<n;i++)
{
for(int j=0;j<n-i;j++)
if(p1[j]>p1[j+1])
{
temp=p1[j];
p1[j]=p1[j+1];
p1[j+1]=temp;
}
}
cout<<p1[n-2]+p1[n-1]<<endl;
}
}