fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
Codeforces Round 1048-B (Div. 2) Cake Collection
최초 업로드: 2025-09-08 18:27:50
최근 수정 시간: 2025-09-08 18:28:06
게시자: rlatjwls3333
카테고리: Codeforces
조회수: 22
# B. Cake Collection [문제 링크](https://codeforces.com/contest/2139/problem/B) ## Problem Statement Maple wants to bake some cakes for Chocola and Vanilla. One day, she discovers $n$ magical cake ovens. The $i$-th oven bakes $a_i$ cakes every second. The cakes remain in their respective ovens until they are collected. At the end of each second, she may teleport to any oven (including the one she is currently at) and collect all the cakes that have accumulated in that oven up to that point. Your task is to determine the maximum number of cakes Maple can collect in $m$ seconds. ## Input Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \leq n \leq 10^5$, $1\leq m\leq 10^8$) — the number of magical ovens and the number of seconds during which Maple will collect cakes. The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \leq a_i \le 10^5$) — the number of cakes the $i$-th oven bakes every second. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. ## Output For each test case, output a single integer representing the maximum number of cakes Maple can collect in $m$ seconds. ## 풀이 정렬해서 그리디하게 제일 큰 수부터 제일 마지막 시간을 할당하고 남는 시간은 버리면 된다. ``` c++ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) { ll n, m; cin >> n >> m; vector<ll> a(n); for(int i=0;i<n;i++) cin >> a[i]; sort(a.begin(), a.end(), greater<ll>()); ll sum=0; for(int i=0;i<n && m>0;i++) sum += a[i]*m--; cout << sum << '\n'; } } ```