Ultraviolet A-10690 Expression Again

Source: Internet
Author: User

Ultraviolet A-10690 Expression Again

Description

Problem C

Expression Again

Input:Standard input
Output:Standard output

TimeLimit:6 seconds

You are given an algebraic expression of the form(X1 + x2 + x3 +... + xn) * (y1 + y2 +... + ym)And(N + m)Integers. Youhave to find the maximum and minimum value of the expression using the givenintegers. For example if you are given(X1 + x2) * (y1 + y2)And you are given1, 2, 3And4. Then maximum value is(1 + 4) * (2 + 3) = 25Whereas minimum value is(4 + 3) * (2 + 1) = 21.

Input

Each input set starts with two positive integersN, M (less than 51). Next line follows(N + M)Integers which are in therange-50To50. Input is terminated by end of file. Therewill be atmost110Testcases.

Output

Output is one line for each case, maximum valuefollowed by minimum value.

SampleInput Outputfor Sample Input
2 2
1 2 3 4
3 1
1 2 3 4
2 2
2 2 2 2

Question: Give You n + m numbers, divide you into n and m, and find the largest and smallest product of their sum.

Idea: Dynamic Planning, set dp [I] [j] to represent the possibility of using I to Form j, and finally solve it from all possibilities

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; const int maxn = 110; int dp [maxn] [maxn * maxn]; int n, m; int main () {while (scanf ("% d", & n, & m )! = EOF) {vector
     
      
Ve (n + m + 1); int sum = 0; for (int I = 1; I <= n + m; I ++) {scanf ("% d ", & ve [I]); sum + = ve [I]; ve [I] + = 50;} memset (dp, 0, sizeof (dp )); dp [0] [0] = 1; for (int I = 1; I <= n + m; I ++) {for (int j = min (I, n); j> = 1; j --) // returns the result of for (int k = 0; k <= 10000; k ++) if (dp [J-1] [k]) dp [j] [k + ve [I] = 1;} int Max =-5000; int Min = 5000; for (int I = 0; I <= 10000; I ++) if (dp [n] [I]) {int tmp = I-50 * n; max = max (Max, tmp * (sum-tmp); Min = min (Min, tmp * (sum-tmp ));} printf ("% d \ n", Max, Min);} return 0 ;}
     
    
   
  
 



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.