bzoj 2697 贪心

时间:2022-01-10 16:49:23

就贪心就行了,首先可以看成n个格子,放物品,那么

一个物品假设放3个,放在1,k,n处,那么价值和放在1,n

是一样的,所以一个物品只放两个就行了,价值大的应该尽量放

在两边,那么排序之后模拟就行了

/**************************************************************
Problem:
User: BLADEVIL
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ //By BLADEVIL
var
n, k :longint;
i :longint;
a :array[..] of longint;
ans :longint; procedure swap(var a,b:longint);
var
c :longint;
begin
c:=a; a:=b; b:=c;
end; procedure qs(low,high:longint);
var
i, j :longint;
xx :longint;
begin
i:=low; j:=high;
xx:=a[(i+j) div ];
while i<j do
begin
while a[i]>xx do inc(i);
while a[j]<xx do dec(j);
if i<=j then
begin
swap(a[i],a[j]);
inc(i); dec(j);
end;
end;
if i<high then qs(i,high);
if j>low then qs(low,j);
end; begin
read(n,k);
for i:= to k do read(a[i]);
qs(,k);
ans:=;
for i:= to k do
begin
if n<= then break;
ans:=ans+(n-)*a[i];
dec(n,);
end;
writeln(ans);
end.