Sticks (backtracking and pruning and local variable issues)

Source: Internet
Author: User

This question is about to cry =
Previously, all the variables were defined as global variables. when the results were submitted, they were always RT and could not be found. Then the local variable is changed to AC =.

Sticks (3.4.2) Time limit:1000 ms Memory limit:10000kb 64bit Io format:% I64d & % i64usubmitstatus

Description

George took sticks of the same length and cut them randomly until all parts became at most 50 units long. now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. please help him and design a program which computes the smallest possible original length of those sticks. all lengths expressed in units are integers greater than zero.

Input

The input contains blocks of 2 lines. the first line contains the number of sticks parts after cutting, there are at most 64 sticks. the second line contains the lengths of those parts separated by the space. the last line of the file contains zero.

Output

The output shoshould contains the smallest possible length of original sticks, one per line.

Sample Input

95 2 1 5 2 1 5 2 141 2 3 40

Sample output

65




# Include <iostream> # include <string. h ># include <algorithm> using namespace STD; int N, num [69]; int Pd [69]; int CMP (int A, int B) {return A> B;} int DFS (INT Len, int S, int Nu) {int I; If (S = 0 & Nu = 0) // if the remaining length is 0 and the remaining number of sticks is 0, return Len; If (S = 0) // if the remaining length is 0, then re-assign S = Len; for (I = 0; I <n; I ++) {If (Pd [I] = 1) continue; if (S> = num [I]) {Pd [I] = 1; if (DFS (Len, S-num [I], nu-1) return Len; pd [I] = 0; // if it fails, the current stick does not use if (Num [I] = S | S = Len) break; while (Num [I] = num [I + 1]) I ++ ;}}return 0 ;}int main () {While (CIN >>> N & N) {int I; int sum; sum = 0; int Len, K; for (I = 0; I <n; I ++) {CIN> num [I]; sum = sum + num [I];} Sort (Num, num + N, CMP); For (LEN = num [0]; Len <= sum; Len ++) {memset (PD, 0, sizeof (PD); If (sum % Len = 0) {k = DFS (Len, 0, n); If (k) break ;}} cout <k <Endl;} return 0;}/* 95 2 1 5 2 1 2 141 2 3 40 */



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.