Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the “kerf”, the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn’t own a saw with which to cut the wood, so he mosies over to Farmer Don’s Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn’t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

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

Output

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

Sample Input

3

8

5

8

Sample Output

34

Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.

The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).

给我一段木头,每次锯断的是他的长度,问我怎么样最小,从小到大排列,5,8,8然后变成了13,8,sum = 13,然后变成了21,sum = 34;

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
#define int long long
int n,sum,x[200005];
int t,p;
priority_queue<int,vector<int>,greater<int> > pq;
signed main()
{
    //freopen("in","r",stdin);
    //freopen("out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    while(cin >> n){
        sum = 0;
        for(int i = 0; i < n; i++){
            cin >> x[i];
            pq.push(x[i]);

        }

        while(pq.size() > 1) {
            t = pq.top();
            pq.pop();
            p = pq.top();
            pq.pop();
            sum += t + p;
            pq.push(t + p);
        }
        cout << sum << endl;
    }
    return 0;
}

R - Fence Repair POJ - 3253的更多相关文章

  1. Fence Repair POJ - 3253 (贪心)

    Farmer John wants to repair a small length of the fence around the pasture. He measures the fence an ...

  2. Fence Repair (POJ 3253)

    农夫约翰为了修理栅栏,要将一块很长的木板切割成N块.准备切成的木板长度为L1.L2.L3...LN,未切割前的木板长度恰好为切割后木板长度的总和.每次切断木板时,需要的开销为这块木板的长度.例如长度为 ...

  3. 贪心算法——Fence Repair(POJ 3253)

    题目描述 农夫约翰为了修理栅栏,要将一块很长的木板切割成N块.准备切成的木板长度为L1,L2,L3--LN,未切割前木板的长度恰好为切割后木板长度的总和.每次切断木板时,需要的开销为这块木板的长度.请 ...

  4. Fence Repair POJ - 3253 哈夫曼思想 优先队列

    题意:给出一段无限长的棍子,切一刀需要的代价是棍子的总长,例如21切一刀 变成什么长度 都是代价21 列如7切成5 和2 也是代价7题解:可以利用霍夫曼编码的思想 短的棍子就放在底层 长的尽量切少一次 ...

  5. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

  6. POJ 3253 Fence Repair (贪心)

    Fence Repair Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  7. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  8. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  9. POJ 3253 Fence Repair (哈夫曼树)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19660   Accepted: 6236 Des ...

随机推荐

  1. MinGW编译dll并引用

    记得某位神仙曾经说过:一个项目不使用dll简直是一场灾难.(滑稽) 这篇文章以A+B/A-B为范例,来介绍如何在MinGW下编译dll并引用. 首先你要安装MinGW,并配置好环境变量(不配置环境变量 ...

  2. td标签内容:换行和不换行设置

    td标签内容:换行和不换行设置 固定td内容不换行:<td style="white-space:nowrap">内容</td>或<td nowrap ...

  3. WIN10 设置WEB

    Web服务器搭建步骤(Win10) 1.在“开始”菜单处打开“控制面板”. 2点击“程序”. 3.点击“启动或关闭Windows功能”. 4.对“Internet Information Servic ...

  4. Nuxt的路由配置以及传参

    Nuxt 路由可以使用a标签进行链接跳转,例如我们创建了一个demo.vue的文件 <p> <a href="/demo">跳转去Demo页面</a& ...

  5. Some series and integrals involving the Riemann zeta function binomial coefficients and the harmonic numbers

    链接:http://pan.baidu.com/s/1eSNkz4Y

  6. radar图生成用户guideline

    1.最后生成的效果是这样的: 2.第一个对话框如下(包含了样图),用于输入维度个数[最小为4,最大不限]: 3.第二个对话框如下,根据维度个数生成了信息录入表[每个维度3个信息,每3个一次重复,不清楚 ...

  7. Spring - MVC - 修改 Java 类后, 触发重启

    1. 概述 学习 Spring MVC 下, 如何可控的触发重启 2. 背景 学习 Spring 场景 有些时候, 改完类, 需要重启 之前有听说, Spring MVC 可以自动重启 于是想, 尝试 ...

  8. Mac配置内网穿透

    闲语: Java开发过程中,往往会对接第三方,而在对接过程中,双方间使用的最多的"通讯"方式就是异步通知.可是异步通知过程中,只能通知到外网地址,可是在调试过程我们都是在本地进行- ...

  9. Java switch的用法

    控制流程语句之→switch选择判断语句  注意事项: 1.多个case后面的值不能重复: 2.switch后面小括号当中只能是下列数据类型: 基本数据类型:byte.short.char.int 引 ...

  10. 使用Python处理非对称加密-测试大佬常用的处理方式

    一.思考❓❔ 1.什么是非对称加密? 公钥加密系统,广泛用于数据加密传输 更详细的解释可参考维基百科( https://en.wikipedia.org/wiki/RSA_(cryptosystem) ...