NOJ1076 Machine Dog assembly cost greedy

Source: Internet
Author: User

Title Description

SED students have recently been fascinated by the manufacture of robotic dogs and have purchased a large number of required parts that can be assembled into a component that can be assembled into a large component. In the manufacture of robotic dogs, components or parts can only be assembled in 22, in the order of assembly. In a robotic dog, each part has an assembly cost, and the cost of assembling one component at a time is the sum of the cost of each part assembly. Given the cost of each part assembly (in yuan), your task is to help sed calculate how much he spends at least.


Input

The first line includes an integer N, which indicates the number of machine dog parts (1≤n≤10000)
The second behavior is n positive integers, which indicates the cost of assembly per machine-dog part (in units of yuan), separated by a space between integers.

Output

The output is only one line, that is, the minimum cost of machine-dog assembly.

Note: The end of the output section requires an extra blank line.


Sample input

10
1 2 3 4 5 6 7 8 9 0

Sample output

136



Thinking of solving problems

The abstract of the topic is that the 22 combination of n numbers can be easily solved with greed by simply knowing that the smallest two parts of the current are grouped together. O (n^2) See Code


#include <cstdio> #include <algorithm> #define LL long long#define INF 1000000010using namespace Std;const int    MAXN = 10010;int S[maxn];int main () {int n;    scanf ("%d", &n);    for (int i = 0; i < n; i + +) scanf ("%d", &s[i]);    ll sum = 0;    if (n = = 1) {printf ("%d\n", S[0]); }else {//????        N-1??        int t = n-1; while (t--) {//??            S??????????????????            int pt1,pt2;            int _min = INF;                    for (int i = 0; i < n; i + +) {if (_min > S[i]) {pt1 = i;                _min = S[i];            }} _min = INF;                    for (int i = 0; i < n; i + +) {if (_min > s[i] && i! = pt1) {pt2 = i;                _min = S[i];            }} sum + = S[pt1] + s[pt2];            S[PT1] = s[pt1] + s[pt2];        S[PT2] = INF;    } printf ("%lld\n", sum); } return 0;} 


NOJ1076 Machine Dog assembly cost greedy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.