CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(medium) » Mixtures

Mixtures

Problem code: MIXTURES

  • Submit
  • All Submissions

All submissions for this problem are available.

Harry Potter has n mixtures in front of him, arranged in a row. Each mixture has one of 100 different colors (colors have numbers from 0 to 99).

He wants to mix all these mixtures together. At each step, he is going to take two mixtures that stand next to each other and mix them together, and put the resulting mixture in their place.

When mixing two mixtures of colors a and b, the resulting mixture will have the color (a+b) mod 100.

Also, there will be some smoke in the process. The amount of smoke generated when mixing two mixtures of colors a and b is a*b.

Find out what is the minimum amount of smoke that Harry can get when mixing all the mixtures together.

Input

There will be a number of test cases in the input.

The first line of each test case will contain n, the number of mixtures, 1 <= n <= 100.

The second line will contain n integers between 0 and 99 - the initial colors of the mixtures.

Output

For each test case, output the minimum amount of smoke.

Example

Input:
2
18 19
3
40 60 20

Output:
342
2400

In the second test case, there are two possibilities:

  • first mix 40 and 60 (smoke: 2400), getting 0, then mix 0 and 20 (smoke: 0); total amount of smoke is 2400
  • first mix 60 and 20 (smoke: 1200), getting 80, then mix 40 and 80 (smoke: 3200); total amount of smoke is 4400

The first scenario is the correct approach since it minimizes the amount of smoke produced.


Author: admin
Date Added: 1-12-2008
Time Limit: 9 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

hrishi @ 24 Jun 2009 08:56 PM

how do we know the input limit??

i0exception @ 24 Jun 2009 10:32 PM

@hrishikesh Read input until you encounter an EOF.

chang @ 18 Jul 2009 10:46 PM

how do I use EOF with scanf ?

admin 2 @ 20 Jul 2009 06:06 PM

scanf() returns the number of characters successfully read from stdin. Use this information to use EOF with scanf.

manib @ 29 Jul 2009 05:47 AM

Can the color of the mixture repeat ?

manib @ 29 Jul 2009 05:48 AM

I meant in a given set, could there be multiple mixtures of the same colors ?

admin 2 @ 29 Jul 2009 06:29 PM

There might be test cases like that.

Can the order of mixtures be

automaton_1 @ 24 Aug 2009 03:19 PM
Can the order of mixtures be circular? For instance, in the above example, can we mix 20 with 40 and the result with 60?

No. They are in a line.

triplem @ 24 Aug 2009 03:26 PM

No. They are in a line.

can we mix 40 and 20 skipping

hemanthp @ 29 Aug 2009 02:23 PM

can we mix 40 and 20 skipping the 60 in between?

I just answered that in the

triplem @ 29 Aug 2009 02:57 PM

I just answered that in the comment directly above yours..

Hello Stephen

sumitb @ 30 Aug 2009 09:30 AM

Hello Stephen

can someone paste some tricky

naveeninkgp @ 10 Nov 2009 10:49 PM

can someone paste some tricky test case for this problem.Have broken my head but can't figure out the problem with my solution.

Have you checked your program

admin @ 11 Nov 2009 02:10 PM

Have you checked your program for values of N for which you can verify the solution using brute-force?

Is there a limit to the size

yellow_agony @ 17 Dec 2009 11:40 AM

Is there a limit to the size in which final answer of smoke would fit in ?

im using while(scanf("%d",&n)

Dalchand @ 24 Dec 2009 12:56 AM

im using

while(scanf("%d",&n) > 0){  // take n inputs and print output }

i guess it should terminate when there is no input?

anybody who has solved this

Geniusguy @ 16 Jan 2010 07:50 PM

anybody who has solved this problem using memoization or using simple recurssion without getting TLE ? ? ?

@Ankush.. dude everyone has

raman bhatia @ 23 Jun 2010 11:32 AM

@Ankush.. dude everyone has solved the problem via DP.. and if you are getting TLE and u think that u have implemented the DP correctly, the only probable reason is that you are not detecting the EOF properly.. thats why your code is never able to stop!!

can ne1 post the solution of

rahul gupta @ 31 Aug 2010 05:42 PM

can ne1 post the solution of this prob...nt able to solve this...me a novice :(

i am learning dp could anyone

dabbcomputers @ 25 Sep 2010 08:36 PM

i am learning dp could anyone tell that is this is dynamic problem or greedy problem and how can i check out that is this dynamic problem or greedy.....???

can u people pls tell me the

khanaleemullah @ 6 Oct 2010 08:47 PM

can u people pls tell me the commands for taking the no if test cases pls...pls

Hey what will be the o/p of

dhiman_nikhil @ 23 Nov 2010 03:36 PM

Hey what will be the o/p of the following i/p

5
40 60 20 9 10

My ans is 2400

Succesfull submitter answers 2870

 

40*60 will be used first since (40+60)%100 gives us 0 that makes rest 0 , so the answer should be 2400

 

Yups answer is 2870 .. i

dhiman_nikhil @ 23 Nov 2010 05:17 PM

Yups answer is 2870 .. i forgot to add rest smoke..

hint : matrix multiplication

anujvermamnit @ 17 Dec 2010 02:58 PM

hint : matrix multiplication problem

please tell me for what input

ravi_26011988 @ 21 Dec 2010 08:14 PM

please tell me for what input it will give incorrect answer.............??

#include<stdio.h>

int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int col[n+1],i,j,min,temp,x,y,k;
unsigned int fume;   
fume=0;
for(i=0;i<n;i++)
{
scanf("%d",&col[i]);
}
i=0;
j=1;
min=1000;

while(n>1)
{
i=0;
j=1;
while(i!=n-1)
{
while(j!=n)
{
temp=(col[i]+col[j])%100;
if(temp<min)
{
min=temp;
x=i;
y=j;
}
j++;
}
i++;
}
fume=fume+col[x]*col[y];
col[x]=min;
min=1000;
for(k=y;k<n-1;k++)
{
col[k]=col[k+1];       
}
n--;
}
printf("%dn",fume);
}           
return 0;
}

sir, will u give me the input

ravi_26011988 @ 21 Dec 2010 08:17 PM

sir,

will u give me the input file for which it gives the incorrect answer............???

can anyone pls tell me if the

iiit @ 19 Jan 2011 10:13 PM

can anyone pls tell me if the following approaach is correct or not

everytime we chose those two adjacent mixtures which giv least value of  (a+b)mod 100 as compared to other pairs

Can you prove that will

triplem @ 20 Jan 2011 08:52 AM

Can you prove that will always lead to the best answer? If you can't prove it, it's normally wrong.

thanx alot stephen ala got AC

iiit @ 21 Jan 2011 11:03 AM

thanx alot stephen ala got AC

@Stephen: Can you please give

bipasaforu @ 24 Jan 2011 09:40 PM

@Stephen: Can you please give me a testcase where my solution is giving the incorrect result?

Hi Stephen, I've tried many

bipasaforu @ 24 Jan 2011 10:41 PM

Hi Stephen, I've tried many testcases and all given correct answers. I'm not sure which one creating the problem. Can you please help me by providing a testcase? 

Your code doesn't even work

triplem @ 25 Jan 2011 09:46 AM

Your code doesn't even work on the sample input.

Hi Stephen, In my end it is

bipasaforu @ 25 Jan 2011 09:55 AM

Hi Stephen, In my end it is perfectly work for the sample input as well as the inputs written in the comments. I also tried with 100 inputs and got the correct result. Can you please let me know the poblem testcase?

Understood the problem. I've

bipasaforu @ 25 Jan 2011 10:01 AM

Understood the problem. I've taken input from console instead of the file.

Nothing is wrong with taking

triplem @ 25 Jan 2011 10:16 AM

Nothing is wrong with taking the input from a console; but your program only outputs 342 for the sample input; it never gets to output 2400 as well.

http://www.codechef.com/views

mayank_gupta @ 2 Mar 2011 06:37 PM

http://www.codechef.com/viewsolution/475504

 

can sum1 tell what is wrong in my solution. It is giving correct answers for the sample input and also of the random inputs. I have checked it with solutions submitted by other persons. But on submitting my solution it shows wrong answer.

Those who are getting wrong

javadecoder @ 16 Mar 2011 05:40 PM

Those who are getting wrong answers,can check some test cases here:https://www.spoj.pl/forum/viewtopic.php?f=3&t=4007&p=16920&hilit=MIXTURES#p16920

760 44 33 67 9 90 34 for this

labham @ 18 Mar 2011 02:39 PM

7
60 44 33 67 9 90 34

for this case on the mention site answer is 8207

and my ans is 6159

60*44+33*67+0*4+4*9+13*90+3*34=6159

it can cum as

60 44 0 9 90 34

04 0 9 90 34

4 9 90 34

13 90 34

3 34

37

 

so explain this answer...

leave it i was checking from

labham @ 18 Mar 2011 02:47 PM

leave it i was checking from wrong soln by mistake..

My solution working proper

jignesh @ 24 Sep 2011 07:26 PM
My solution working proper for all case given in above link. http://www.codechef.com/viewsolution/672323 But still I am getting wrong ans.....

same here. i am getting right

tpawan @ 18 Dec 2011 08:29 PM
same here. i am getting right ans for above mentioned all test cases. dont know why its showing wrong answer. @admin : can you plz tell me for what input my code is giving wrong answer????

Hi.. My program is working

comvidyarthi @ 20 Dec 2011 12:09 PM
Hi.. My program is working fine for all the inputs given in the link above.. Also it is behaving as expected for input from a file as well.. But stil I am getting WA. for N=1 I have returned the number as it is for N=2 I simply return the product of the 2 numbers Can anyone tell me if there might be any other case I am missing?

Make sure you handle the case

sugar.angel @ 12 Jan 2012 07:46 PM
Make sure you handle the case when N=1. Return 0 in that case. Took a long time to figure out.

Given in the

abhinav1592 @ 25 May 2012 05:44 PM
Given in the question...>Second possiblity of 2nd test case is "first mix 60 and 20 (smoke: 1200), getting 80, then mix 40 and 80 (smoke: 3200); total amount of smoke is 4400". My question is : If we mix 60 and 20 how we get 80 as color...shouldn't we get 0(because according to question color after mixing is (a+b) mod 100...and smoke=1200)...and then we mix 40 and 0 to get color=0 and smoke=0??

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com