首页 > 其他分享 >CodeForces 34B Sale

CodeForces 34B Sale

时间:2023-04-20 23:31:57浏览次数:55  
标签:34B TV CodeForces Sale int sets Input Bob include


B - Sale


Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u


Submit  Status  Practice  CodeForces 34B


Description



Once Bob got to a sale of old TV sets. There were n TV sets at that sale. TV set with index i costs ai bellars. Some TV sets have a negative price — their owners are ready to pay Bob if he buys their useless apparatus. Bob can «buy» any TV sets he wants. Though he's very strong, Bob can carry at most m



Input



The first line contains two space-separated integers n and m (1 ≤ m ≤ n ≤ 100) — amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains n space-separated integers ai ( - 1000 ≤ ai ≤ 1000) — prices of the TV sets.



Output



Output the only number — the maximum sum of money that Bob can earn, given that he can carry at most m



Sample Input



Input



5 3 -6 0 35 -2 4



Output



8



Input



4 2 7 0 0 -7



Output



7







很水的一个题,看数据猜题意过的,先排序然后选前m小的数,如果前m个数有大
于0的数则提前终止,最后把前m小的数字相加在乘以-1,得到一个正数输出就可以了






AC代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
    int n,m;
    int a[10001];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        int sum = 0;
        for(int i=0;i<m;i++)
        {
            if(a[i]<0)
            {
                sum = sum + (-1*a[i]);
            }
            else
            {
                break;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

标签:34B,TV,CodeForces,Sale,int,sets,Input,Bob,include
From: https://blog.51cto.com/u_14834528/6210897

相关文章

  • C. Table Decorations(Codeforces)(规律)
    C.TableDecorationstimelimitpertestmemorylimitpertestinputoutputr red, g greenand b blueballoons.Todecorateasingletableforthebanquetyo......
  • CodeForces 34A Reconnaissance 2
     Reconnaissance2TimeLimit:2000MS     MemoryLimit:262144KB     64bitIOFormat:%I64d&%I64uSubmit Status Practice CodeForces34ADescriptionn soldiersstandinacircle.Foreachsoldierhisheight ai isknown.Areco......
  • codeforces round The Monster and the Squirrel 529B (数学规律)
    TheMonsterandtheSquirrelTimeLimit: 1000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionArithemonsteralwayswakesupveryearlywiththefirstrayofthesunandthefirstthingshedoesisfeedinghersqu......
  • Codeforces Round 850 (Div. 2, based on VK Cup 2022 - Final Round) E. Monsters (h
    传送门详细题解传送门  抄的ygg代码,向在这里说一下刚开始没看懂的部分。  求答案的时候是把所有的当前为止的所有数值加起来减去一个从1开始并且公差为1的等差数列的前size项和。其中size是当前最多能用到哪个位置,满足前size项能构成1,2,3,....,sz这样的形式。  假设我们......
  • CodeForces - 610B Vika and Squares (模拟)
    CodeForces-610BVikaandSquaresTimeLimit: 2000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionVikahas n jarswithpaintsofdistinctcolors.Allthejarsarenumberedfrom 1 to n andthe......
  • CodeForces - 659C Tanya and Toys (map&模拟)
    CodeForces-659CTanyaandToysTimeLimit: 1000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionInBerlandrecentlyanewcollectionoftoyswentonsale.Thiscollectionconsistsof 109 typesof......
  • CodeForces - 367B Sereja ans Anagrams (map)
    CodeForces-367BSerejaansAnagramsTimeLimit: 1000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionSerejahastwosequences a and b andnumber p.Sequence a consistsof n integers a1, a......
  • CodeForces - 368C Sereja and Algorithm (找规律&模拟)
    CodeForces-368CSerejaandAlgorithmTimeLimit: 1000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionSerejalovesallsortsofalgorithms.Hehasrecentlycomeupwithanewalgorithm,whichreceiv......
  • CodeForces - 616E Sum of Remainders (数论)大数取余求和 好题
    CodeForces-616ESumofRemaindersTimeLimit: 2000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionCalculatethevalueofthesum: nmod1 + nmod2 + nmod3 +...+ nmodm.Astheresultcanbeve......
  • 通过Salesforce考试 (考证)后,如何在Trailhead上验证和维护证书?
    随着Salesforce产品家族的不断壮大,学习者可以考的认证也在不断增多。从十几年前的几个认证,增长到现在的40多个认证。在获得Salesforce认证之后,除了要将其放在LinkedIn和Trailblazer.me个人资料中,还有一种官方途径可以让其他人确认你确实获得了这些认证,这也是你检查是否需要进行......