思路
规定
f
[
i
]
f[i]
f[i]为前i个人已经过河,
a
[
i
]
,
表
示
第
i
个
人
所
用
时
间
a[i],表示第i个人所用时间
a[i],表示第i个人所用时间
容易想到每次我们用时间消耗小的把船给开会去,这里先从小到大排序;
题目举例中给了我们一种转移方式,利用
f
[
i
−
2
]
f[i-2]
f[i−2]进行转移,首先先让第一个人回去,时间消耗为a[1],再让第i-1,i个人开船去对岸时间消耗为a[i],再让第2个人开船回去接第一个人去对岸消耗为2*a[2],总消耗为
f
[
i
−
2
]
+
a
[
1
]
+
a
[
i
]
+
a
[
2
]
∗
2
f[i-2]+a[1]+a[i]+a[2]*2
f[i−2]+a[1]+a[i]+a[2]∗2;
用
f
[
i
−
1
]
f[i-1]
f[i−1]转移为:
f
[
i
−
1
]
+
a
[
1
]
+
a
[
i
]
f[i-1]+a[1]+a[i]
f[i−1]+a[1]+a[i]
状态转移方程为
f
[
i
]
=
m
i
n
(
f
[
i
−
1
]
+
a
[
1
]
+
a
[
i
]
,
f
[
i
−
2
]
+
a
[
i
]
+
a
[
1
]
+
a
[
2
]
∗
2
)
f[i] = min(f[i-1] + a[1] + a[i],f[i-2] + a[i]+ a[1]+ a[2]*2)
f[i]=min(f[i−1]+a[1]+a[i],f[i−2]+a[i]+a[1]+a[2]∗2)
/*
author:@bzdhxs
date:2022/1/22
URL:https://ac.nowcoder.com/acm/contest/24213#question
线性dp;
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
template <typename T>
inline void read(T &s){s = 0;T w = 1, ch = getchar();while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }while (isdigit(ch)) { s = (s << 1) + (s << 3) + (ch ^48); ch = getchar();} s *= w;}
template <typename T>
inline void write(T s){if (s < 0) putchar('-'), s = -s;if (s > 9) write(s / 10);putchar(s % 10 + '0');}
#define int long long
#define _orz ios::sync_with_stdio(false),cin.tie(0)
#define mem(str,num) memset(str,num,sizeof(str))
#define forr(i,a,b) for(int i = a; i <= b;i++)
#define forn(i,n) for(int i = 0; i < n; i++)
#define dbg() cout <<"0k!"<< endl;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e5+10;
int f[N];
signed main()
{
int n; cin >> n;
vector<int> a(n+1,0);
forr(i,1,n) cin >> a[i];
sort(a.begin(),a.end());
mem(f,inf);f[0] = 0;
f[1] = a[1];
f[2] = a[2];
forr(i,3,n){
f[i] = min(f[i-1] + a[1] + a[i],f[i-2] + a[i]+ a[1]+ a[2]*2);
}
cout << f[n] << endl;
return 0;
}