Home 백준 온라인 판매 (1246)
Post
Cancel

백준 온라인 판매 (1246)

백준 온라인 판매 (1246)

스크린샷 2022-02-04 오후 4 36 26

현재 가지고 있는 달걀 수를 넘지 않게 최대 이익을 계산 해주면 되는 문제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.io.*;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

class FastScanner {
    BufferedReader br;
    StringTokenizer st;

    public FastScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }
}

public class Main {
    public static void  main(String args[]) throws IOException{
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        FastScanner sc = new FastScanner();

        int eggNum = sc.nextInt();
        int customerNum = sc.nextInt();

        PriorityQueue<Integer> customer = new PriorityQueue<Integer>(Collections.reverseOrder());

        int max = Integer.MIN_VALUE;
        int price = 0;

        for(int i=0; i<customerNum; i++)
            customer.add(sc.nextInt());

        int maxSellNum = (customer.size() >= eggNum) ? eggNum : customer.size();

        for(int i=0; i<customerNum; i++){
            int tmp = (i+1 >= maxSellNum) ? maxSellNum : i+1;     // 팔 수 있는 최대 달걀 수를 넘지 않게 해주는 수식
            int curPrice = customer.peek() * tmp;           // 현재 가격으로 얻을 수 있는 최대 이익 계산

            if(max <= curPrice) {
                max = curPrice;
                price = customer.poll();
            }else
                customer.poll();
        }

        bw.write(Integer.toString(price ) + " " +  Integer.toString(max) + "\n");
        bw.close();
    }
}

This post is licensed under CC BY 4.0 by the author.