Codeforces 484D-kindergarten

本文探讨了一个幼儿园孩子分组的问题,旨在通过算法找到一种分组方式,使得每组孩子的最大魅力值差达到整体最大值。文章提供了一段实现该目标的C++代码。

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

Kindergarten
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maximum difference of charisma of two children in the group (in particular, if the group consists of one child, its sociability equals a zero).

The teacher wants to divide the children into some number of groups in such way that the total sociability of the groups is maximum. Help him find this value.

Input

The first line contains integer n — the number of children in the line (1 ≤ n ≤ 106).

The second line contains n integers ai — the charisma of the i-th child ( - 109 ≤ ai ≤ 109).

Output

Print the maximum possible total sociability of all groups.

Examples
input
5
1 2 3 1 2
output
3
input
3
3 3 3
output
0
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 1e6+5;

int n, a[maxn];
long long int dp[maxn][2];

int main ()
{
    scanf("%d%d",&n,&a[1]);
    for (int i=2;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]>a[i-1])
        {
            dp[i][0]=max(dp[i-1][1],dp[i-1][0]+a[i]-a[i-1]);
            dp[i][1]=max(dp[i-1][1],dp[i-1][0]);
        }
        else
        {
            dp[i][1]=max(dp[i-1][0],dp[i-1][1]+a[i-1]-a[i]);
            dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
        }
    }
    printf("%lld\n", max(dp[n][0], dp[n][1]));
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值