Codeforces 12C Fruits

Source: Internet
Author: User
Tags integer numbers

FruitsTime limit:1000MS Memory Limit:262144KB 64bit IO Format:%i64d &%i6 4u SubmitStatusPracticecodeforces 12C


The spring is coming and it means, a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of m fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times.

When he came to the fruit stall of Ashot, he saw that the seller Hadn ' t distributed price tags to the goods, and put all P Rice tags on the counter. Later Ashot would attach every price tags to some kind of fruits, and Valera would be able to count the total price of all FR Uits from the his list. But Valera wants to know now what can is the smallest total price (in case of the most«lucky»for him distribution of the PRI CE tags) and the largest total price (in case of the most«unlucky»for him distribution of price tags).


The first line of the input contains n  and  m   ( 1≤ n , m ≤100)-the number of price tags (which are equal to the number of Diffe Rent kinds of fruits that Ashot sells) and the number of items in Valera ' s list. The second line Contains  n  space-separated positive integer numbers. Each of them doesn ' t exceed 100 and stands for the price of one fruit of some kind. The Following  m  lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn ' t exceed 32. It is guaranteed, the number of distinct fruits from the list was less of equal To  n. Also It is known that the seller have in stock all fruits that Valera wants to buy.


Print numbers a and b (ab)-the minimum and the maximum possible s Um which Valera may need to buy all fruits from the his list.

Sample Input

5 3
4 2 1) 10 5
7 19
6 5
3 5 1 6 8 1
11 30
1#include <stdio.h>2#include <string.h>3#include <algorithm>4 using namespacestd;5 6 intMain ()7 {8     intn,m;9     inti,j,k;Ten     intprice[ the],namss[ the]; One     Charnam[ the][ $]; A      while(SCANF ("%d%d", &n,&m)! =EOF) -     { -memset (NAMSS,0,sizeof(NAMSS)); the          for(i=1; i<=n;i++) -scanf"%d",&price[i]); -Sort (price+1, price+n+1); -          for(i=1; i<=m;i++) +         { -k=0; +scanf"%s",&nam[i]); A              for(j=1; j<i;j++) at             { -                 if(strcmp (nam[i],nam[j]) = =0) -                 { -namss[j]++; -k=1; i--;m--; -                      Break; in                 } -             } to             if(k==0) +             { -namss[i]++; the             } *         } $Sort (namss+1, namss+m+1);Panax Notoginseng         /*For (i=1;i<=n;i++) - printf ("%d", Price[i]); the printf ("\ n"); + For (i=1;i<=m;i++) A printf ("%d", namss[i]); the printf ("\ n");*/ +         intans1=0, ans2=0; -          for(i=m;i>=1; i--) $         { $ans1=ans1+namss[i]*price[m-i+1]; -ans2=ans2+namss[i]*price[n-m+i]; -         } theprintf"%d%d\n", ans1,ans2); -     }Wuyi     return 0; the}
View Code

Codeforces 12C Fruits

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: 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.