Line 1: One integer N, the number of planks
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
输出
Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts
样例输入
3858
样例输出
34
代码
#include <iostream>
#include <algorithm>
#include <cstdio>
typedef long long ll;
using namespace std;
int l[20006];
int main(){
int N;scanf("%d",&N);for(int i =0;i<N;i++){scanf("%d",&l[i]);}
ll ans =0;//遍历至只剩一块板while(N>1){//找到最短的两块板
int m1 =0,m2 =1;if(l[m1]>l[m2])swap(m1,m2);for(int i =2;i<N;i++){if(l[i]<l[m1]){
m2 = m1;
m1 = i;}elseif(l[i]<l[m2]){
m2 = i;}}//合并
int t = l[m1]+l[m2];
ans = ans+t;//用合并的板替换掉之前两块if(m1 ==N-1)swap(m1,m2);
l[m1]= t;
l[m2]= l[N-1];N--;}printf("%lld\n",ans);return0;}
题目描述输入Line 1: One integer N, the number of planks Lines 2..N+1: Each line contains a single integer describing the length of a needed plank输出Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts样例输入38 5 8样例输出34代码#