Dessert Wizard Problem

在炎热的夏天,厨师为了吸引更多的顾客,利用巫师的魔法帮助制作了一道美味佳肴。通过精心选择和组合食材,厨师在两阶段中最大化了食物的美味值。本文详细介绍了制作过程和优化策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dessert Wizard

Problem code: DELISH

It's finally summer in Chefland! So our chef is looking forward to prepare some of the best "beat-the-heat" dishes to attract more customers. He summons the Wizard of Dessert to help him with one such dish.

The wizard provides the chef with a sequence of N ingredients where the ith ingredient has a delish value of D[i]. The preparation of the dish takes place in two phases.



Phase 1 : The chef chooses two indices i and j and adds the ingredients i, i+1, ..., j to his dish. He also finds the sum of the delish value in this range i.e D[i] + D[i+1] + ... + D[j].



Phase 2 : The chef chooses two more indices k and l and adds the ingredients k, k+1, ..., l to his dish. He also finds the sum of the delish value in this range i.e D[k] + D[k+1] + ... + D[l].


Note that 1ij < klN.





The total delish value of the dish is determined by the absolute difference between the values obtained in the two phases. Obviously, the chef wants to maximize the total delish value for his dish. So, he hires you to help him.

Input

First line of input contains an integer T denoting the number of test cases. For each test case, the first line contains an integer N denoting the number of ingredients. The next line contains N space separated integers where the ith integer represents the delish value D[i] of the ith ingredient.

Output

Print the maximum delish value of the dish that the chef can get.

Constraints

  • 1T50
  • 2N10000
  • -1000000000 (−109)D[i] 1000000000 (109)

Example

Input:
2
5
1 2 3 4 5
4
1 1 -1 -1

Output:
13
4

Explanation

Example case 1.

Chef can choose i = j = 1, k = 2, l = 5.

The delish value hence obtained is | (2+3+4+5) − (1) | = 13 .

Example case 2.

Chef can choose i = 1, j = 2, k = 3, l = 4.

The delish value hence obtained is | ( ( −1 ) + ( −1 ) ) − ( 1 + 1 ) | = 4 .


/*思路:分别求得从0-n序列的最大值和最小值,保存在max1[] , min2[]内,
    再求得n-0序列的最大值和最小值,保存在max2[] , min1[]内
最后只要找到max{abs(max1[] - min1[] ) , (max2[] - min2[] )}即可;

*/
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std ;
#define MAX 10004
#define inf 1000000000
long long int min1[MAX] , min2[MAX] , max1[MAX] , max2[MAX] , a[MAX];

/*求0-n之间的最大值*/
void find_Max(int n )
{
    long long int i , sum = 0 , m = -inf , sum1 = 0 , m1 = inf ;
    for( i = 0 ; i < n ; i ++ )
    {

        sum1 += a[i] ;
        if(sum1 < m1 )
            m1 = sum1 ;
        if(sum1 > 0 )
            sum1 = 0 ;
        min2[i] = m1 ;

        sum += a[i] ;
        if( sum > m )
            m = sum ;
        if(sum < 0 )
            sum = 0 ;
        max1[i] = m ;

    }
    return ;
}

/*求n-0之间的最小值*/
void find_Min(int n )
{
    long long int i , sum = 0 , m = inf , sum1 = 0 , m1 = -inf  ;
    for( i = n-1 ;i >=0 ; i --)
    {
        sum +=a[i] ;
        if(sum < m )
            m = sum ;
        if(sum > 0 )
            sum = 0 ;
        min1[i] = m ;

        sum1 += a[i] ;
        if(sum1 > m1 )
            m1 = sum1;
        if(sum1 < 0 )
            sum1 = 0 ;
        max2[i] = m1 ;


    }
    return ;
}

int main()
{
    int t ;
    scanf("%d",&t);
    while(t--){
        int n ;
        scanf("%d",&n);
        int i ;
        for( i = 0 ; i < n ; i ++)
            cin >> a[i] ;
        memset(max1 , 0 , sizeof(max1)) ;
        memset(min1 , 0 , sizeof(min1)) ;
        memset(max2 , 0 , sizeof(max2)) ;
        memset(min2 , 0 , sizeof(min2)) ;
        find_Max(n);
        find_Min(n);

        long long int sum = 0 ;
        for( i = 0 ; i < n-1 ; i ++){
            if(sum < abs(max1[i] - min1[i + 1]))
                sum = abs(max1[i] - min1[i + 1]);
            if(sum < abs(max2[i + 1] - min2[i ]))
                sum = abs(max2[i + 1] - min2[i ]);
           // cout<<max1[i]<<" "<<min1[i + 1] <<"    "<<max2[i + 1]<<" "<<min2[i] << endl;
        }
        printf("%lld\n",sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值