1038D. Slime(思维)

本文探讨了一个关于多个史莱姆互相吞噬以求得最终数值最大的问题。通过分析不同数值组合下最优解的策略,给出了具体的算法实现。适用于对算法博弈论感兴趣的读者。

There are n

slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it.

Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists).

When a slime with a value x

eats a slime with a value y, the eaten slime disappears, and the value of the remaining slime changes to x−y

.

The slimes will eat each other until there is only one slime left.

Find the maximum possible value of the last slime.

Input

The first line of the input contains an integer n

(1≤n≤500000

) denoting the number of slimes.

The next line contains n

integers ai (−109≤ai≤109), where ai is the value of i

-th slime.

Output

Print an only integer — the maximum possible value of the last slime.

Examples

Input

Copy

4
2 1 2 1

Output

Copy

4

Input

Copy

5
0 -1 -1 -1 -1

Output

Copy

4

Note

In the first example, a possible way of getting the last slime with value 4

is:

  • Second slime eats the third slime, the row now contains slimes 2,−1,1
  •  
  • Second slime eats the third slime, the row now contains slimes 2,−2
  •  
  • First slime eats the second slime, the row now contains 4
  •  

In the second example, the first slime can keep eating slimes to its right to end up with a value of 4

.题意:

题意是有n个史莱姆,每一个史莱姆都可以吃掉相邻的史莱姆,使得自己的值改变,当x吃掉y的时候,x的值变为x-y,问最后所剩下的史莱姆最大是多少。

思路:

  三种情况,第一种是所有数都是正数,对于这种情况我们要找一个最小值去减去一个数来获得一个负数,然后用这个负数去减别的正数从而获得更小的负数,最后再用一个正数来减去这个负数从而获得最大值,在这个过程中我们可以发现所有值最终都是被加上的,除了最小值第一次减的时候没有加上以外还少算了一次最小值,所以最终ans就是所有数的和减去2*Min,解释的不太好理解,可以写一下这个过程想想,同理当所有数都是负数的时候就是所有数的绝对值的和减去2*Max,当既有正数又有负数的时候就是输出所有元素的绝对值的和,然后就是对于n等于1和2的情况特判一下就好了。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define Inf 0x7f7f7f7f
using namespace std;
typedef long long LL;
const int N = 500005;
LL arr[N];
int n;
int main(){
	scanf("%d",&n);
	LL mint=Inf,maxt=-Inf;
	LL ans=0;
	for(int i=0;i<n;i++){
		scanf("%lld",&arr[i]);
		ans+=abs(arr[i]);
		mint=min(mint,arr[i]);
		maxt=max(maxt,arr[i]);	
	}
	if(n==1) printf("%lld\n",arr[0]);
	else if(n==2)
		printf("%lld\n",max(arr[0]-arr[1],arr[1]-arr[0]));
	else{
		if(mint>0) ans-=2*mint;
		else if(maxt<0) ans-=2*abs(maxt);
		printf("%lld\n",ans);
	}
	return 0;
}

 

Table of Contents 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2.1 2.2 Supported Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Downloading SLIME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.1 Downloading from Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.2 Git incantations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3.1 Installing from Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4 Running SLIME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5 Setup Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5.1 Basic customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5.2 Multiple Lisps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.5.3 Loading Swank faster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2 2 2 3 3 3 3 4 4 4 5 Using Slime mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.1 User-interface conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.1.1 Temporary buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.1.2 *inferior-lisp* buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.1.3 Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.1.4 Key bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.2 Evaluation commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 3.3 Compilation commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3.4 Completion commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3.5 Finding definitions (“Meta-Point” commands). . . . . . . . . . . . . . . . . 10 3.6 Documentation commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.7 Cross-reference commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 3.7.1 Xref buffer commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.8 Macro-expansion commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.9 Disassembly commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.10 Abort/Recovery commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 3.11 Inspector commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 3.12 Profiling commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 3.13 Shadowed Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3.14 Semantic indentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3.15 Reader conditional fontification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 4 SLDB: the SLIME debugger. . . . . . . . . . . . . . . . . . 17 4.1 4.2 4.3 4.4 4.5 Examining frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Invoking restarts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Navigating between frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Stepping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Miscellaneous Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 17 18 18 19ii 5 Misc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 5.1 slime-selector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 5.2 slime-macroexpansion-minor-mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 5.3 Multiple connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 6 Customization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 6.1 Emacs-side . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.1.1 Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2 Lisp-side (Swank) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2.1 Communication style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2.2 Other configurables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Tips and Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 7.1 Connecting to a remote lisp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1.1 Setting up the lisp image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1.2 Setting up Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1.3 Setting up pathname translations . . . . . . . . . . . . . . . . . . . . . . . . . 7.2 Globally redirecting all IO to the REPL . . . . . . . . . . . . . . . . . . . . . . . 7.3 Connecting to SLIME automatically . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 23 23 24 24 25 27 27 28 28 28 29 Contributed Packages . . . . . . . . . . . . . . . . . . . . . . . . . 30 8.1 Loading Contrib Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.1.1 Loading and unloading “on the fly” . . . . . . . . . . . . . . . . . . . . . . . 8.2 REPL: the “top level”. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.2.1 REPL commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.2.2 Input navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.2.3 Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.3 Multiple REPLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.4 inferior-slime-mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.5 Compound Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.6 Fuzzy Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.6.1 The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.6.2 Duplicate Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.7 slime-autodoc-mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.8 ASDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.9 Banner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.10 Editing Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.11 Fancy Inspector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.12 Presentations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.13 Typeout frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.14 TRAMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.15 Documentation Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.16 Xref and Class Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.17 Highlight Edits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.18 Scratch Buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.19 SLIME Trace Dialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.20 slime-sprof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.21 Meta package: slime-fancy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 30 30 31 32 32 33 33 34 35 35 35 36 37 38 38 38 39 41 41 41 42 42 42 42 45 45iii 9 Credits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Hackers of the good hack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Thanks! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Key (Character) Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Command and Function Index . . . . . . . . . . . . . . . . . . . 50 Variable and Concept Index . . . . . . . . . . . . . . . . . . . . . . 52
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值