* One second to Warren (if you have a prophet's powers)

Source: Internet
Author: User

1010:stock
Time limit:1 Sec Memory limit:128 MB 64bit IO Format:%lld
submitted:357 accepted:67
[Submit] [Status] [Web Board]
Description

Fcbruce recently struggled in the stock market, and he decided to go back to Xie (MA) when he finished the two votes. Fcbruce stared at WUSTACM's stock, and the Night (Shen) (jing) star (Wang) Elephant (Luo) predicted the next N-day price. Fcbruce can perform up to two full trades (one transaction needs to be bought first and then sold), and the stock held before the second buy must be sold, since Fcbruce is a poor man who can only trade one share at a time. He now wants to know how much money he can make.

Input

Multiple sets of test data.

The first line is the number of data groups T (T < 100), which indicates that there is a T group of test data.

The first line of each set of test data is an integer n (0 <= N <), the meaning of n is given above.

The second line has n integers, and the first integer represents the stock price of day I (a nonnegative integer less than 1000).

Output

For each set of test data, a separate output line represents the maximum amount of money that is earned through stock trading.

Sample Input

2
5
1 2 3) 4 5
5
4 6 5) 1 7

Sample Output

4
8

Ah, this is my second dynamic planning of the topic, at first very difficult to understand, and then after my multi-inquiry, searching around to finally understand the problem, the impression is deep ah ... Thank for my drawing the person, otherwise I can not understand, this slag slag understanding ability is very low, ah hahaha haha ...
to tell the truth, after the submission is very guilty ah ...

#include <stdio.h> int main () {int t,n;
            while (scanf ("%d", &t)!=eof) {while (t--) {int a[1005]= {0},b[1005]= {0},c[1005]= {0};
            scanf ("%d", &n);
                if (!n) {printf ("0\n");
            Continue
            } int t,x=0;
            for (int i=0; i<n; i++) scanf ("%d", &a[i]);
            T=A[0];
                for (int i=1; i<n; i++)//Search from the go, the resulting value must be positive {t=t<a[i]?t:a[i];//keep the minimum value, so that the difference with the subsequent to get the maximum
            B[i]=b[i-1]> (A[I]-T) b[i-1]:(a[i]-t);
            } T=a[n-1];
                for (int i=n-2; i>=0; i--)//Search from backward forward, the resulting value must be negative {t=t>a[i]?t:a[i];
                c[i]=c[i+1]< (A[I]-T) c[i+1]:(a[i]-t);
            X=x> (B[i]-c[i])? x: (B[i]-c[i]);//max minus minimum value must be the maximum value, and there will be no repetition, the key point in this Ah, at each point will be divided into two parts of the sequence, there will be no cross, guaranteed to buy the next stock, the hands of the previous stock has been sold.
        } printf ("%d\n", X);

 }   } return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.