Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
p00881 Twenty Questions
Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature. You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question. You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning. The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable. Input The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects. The end of the input is indicated by a line containing two zeros. There are at most 100 datasets. Output For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result. Example Input 8 1 11010101 11 4 00111001100 01001101011 01010000011 01100110001 11 16 01000101111 01011000000 01011111001 01101101001 01110010111 01110100111 10000001010 10010001000 10010110100 10100010100 10101010110 10110100010 11001010011 11011001001 11111000111 11111011101 11 12 10000000000 01000000000 00100000000 00010000000 00001000000 00000100000 00000010000 00000001000 00000000100 00000000010 00000000001 00000000000 9 32 001000000 000100000 000010000 000001000 000000100 000000010 000000001 000000000 011000000 010100000 010010000 010001000 010000100 010000010 010000001 010000000 101000000 100100000 100010000 100001000 100000100 100000010 100000001 100000000 111000000 110100000 110010000 110001000 110000100 110000010 110000001 110000000 0 0 Output 0 2 4 11 9
7
0
#include<iostream> #include<vector> #include<utility> #include<bitset> #include<functional> using namespace std; int main(int argc, char *argv[]) { for(int t = 1;; t++) { int n, m; cin >> m >> n; if(n == 0) break; vector<int> os; for(int i = 0; i < n; i++) { string str; cin >> str; int o = 0; for(int k = 0; k < m; k++) { o = (o << 1) | (str[k] == '1'); } os.push_back(o); } const int M = 1 << m; // dp: state = a set of objects to be determined (too big, in general) // = m bits of fixed (asked so far) or not yet fixed // i.e., each bit = 0 | 1 | not_fixed_yet = 10,11,0x (2bit) // = O(3^m) (or O(4^m) for the 2bit representation) vector<vector<int>> memo(M, vector<int>(M,-1)); function<int(int, int)> rec = [&rec,&os,&memo,&m,&M](const int fixed, const int bits) { // memo if(memo[fixed][bits] >= 0) return memo[fixed][bits]; int n = 0; // # of objects to be determined for(int o : os) { n += ((o & fixed) == bits) ? 1 : 0; } if(n == 1) { // already determined memo[fixed][bits] = 0; return memo[fixed][bits]; } // try all possible choices of next bits int mn = m; int bs = (~fixed)&(M-1); while(bs != 0) { int b = -bs&bs; bs ^= b; mn = min(mn, 1+max(rec(fixed | b, bits), rec(fixed | b, bits | b))); } memo[fixed][bits] = mn; return memo[fixed][bits]; }; cout << rec(0,0) << endl; } return 0; } /* This is wrong. // greedy algo.; choosing the most informative bit to split the object set function<int(const vector<int>&, int)> rec = [&rec](const vector<int> &os, const int bits) { int n = os.size(); if(n == 1) return 0; int bit = 0; int mx = 0; int bs = bits; while(bs != 0) { int b = -bs&bs; bs ^= b; int cnt = 0; for(int i = 0; i < n; i++) { if(os[i] & b) { cnt ++; } } cnt = min(n - cnt, cnt); if(cnt > mx) { mx = cnt; bit = b; } } vector<int> ones, zeros; for(int i = 0; i < n; i++) { if(os[i] & bit) { ones.push_back(os[i]); } else { zeros.push_back(os[i]); } } return 1+max(rec(ones, bits ^ bit), rec(zeros, bits ^ bit)); }; cout << rec(os, (1<<m) - 1) << endl; */
CPP
p00881 Twenty Questions
Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature. You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question. You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning. The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable. Input The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects. The end of the input is indicated by a line containing two zeros. There are at most 100 datasets. Output For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result. Example Input 8 1 11010101 11 4 00111001100 01001101011 01010000011 01100110001 11 16 01000101111 01011000000 01011111001 01101101001 01110010111 01110100111 10000001010 10010001000 10010110100 10100010100 10101010110 10110100010 11001010011 11011001001 11111000111 11111011101 11 12 10000000000 01000000000 00100000000 00010000000 00001000000 00000100000 00000010000 00000001000 00000000100 00000000010 00000000001 00000000000 9 32 001000000 000100000 000010000 000001000 000000100 000000010 000000001 000000000 011000000 010100000 010010000 010001000 010000100 010000010 010000001 010000000 101000000 100100000 100010000 100001000 100000100 100000010 100000001 100000000 111000000 110100000 110010000 110001000 110000100 110000010 110000001 110000000 0 0 Output 0 2 4 11 9
7
0
#include <bits/stdc++.h> using namespace std; constexpr int INF = 1 << 30; void chmin(int &a, int b){ a = min(a, b); } int cnt[1<<11][1<<11], dp[1<<11][1<<11]; int solve(){ int m, n; cin >> m >> n; if(m==0) return 1; vector<int> a(n); for(int i=0;i<n;i++){ string s; cin >> s; for(int j=0;j<m;j++){ a[i] |= (s[j]-'0')<<j; } } for(int S=0;S<1<<m;S++){ for(int T=S;;T=(T-1)&S){ cnt[S][T] = 0; dp[S][T] = INF; for(int i=0;i<n;i++){ if((S&a[i])==T) cnt[S][T]++; } if(T==0) break; } } for(int S=(1<<m)-1;S>=0;S--){ for(int T=S;;T=(T-1)&S){ if(cnt[S][T]<=1){ dp[S][T] = 0; }else{ for(int i=0;i<m;i++){ if((S>>i)&1) continue; chmin(dp[S][T], max(dp[S|1<<i][T], dp[S|1<<i][T|1<<i])+1); } } if(T == 0) break; } } cout << dp[0][0] << endl; return 0; } int main(){ while(solve()==0); return 0; }
CPP
p00881 Twenty Questions
Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature. You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question. You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning. The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable. Input The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects. The end of the input is indicated by a line containing two zeros. There are at most 100 datasets. Output For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result. Example Input 8 1 11010101 11 4 00111001100 01001101011 01010000011 01100110001 11 16 01000101111 01011000000 01011111001 01101101001 01110010111 01110100111 10000001010 10010001000 10010110100 10100010100 10101010110 10110100010 11001010011 11011001001 11111000111 11111011101 11 12 10000000000 01000000000 00100000000 00010000000 00001000000 00000100000 00000010000 00000001000 00000000100 00000000010 00000000001 00000000000 9 32 001000000 000100000 000010000 000001000 000000100 000000010 000000001 000000000 011000000 010100000 010010000 010001000 010000100 010000010 010000001 010000000 101000000 100100000 100010000 100001000 100000100 100000010 100000001 100000000 111000000 110100000 110010000 110001000 110000100 110000010 110000001 110000000 0 0 Output 0 2 4 11 9
7
0
#include <bits/stdc++.h> using namespace std; int pow3[12] = {1}, n, m; int dp[177147], cnt[177147]; int edcode(vector<int> S) { int ans = 0; for (int i = 0; i < 11; ++i) { ans += S[i] * pow3[i]; } return ans; } vector<int> decode(int S) { vector<int> ans; for (int i = 0; i < 11; ++i, S /= 3) { ans.push_back(S % 3); } return ans; } int dfs(int S) { if (dp[S] != -1) return dp[S]; else if (cnt[S] == 1) return 0; else { auto c = decode(S); dp[S] = INT_MAX; for (int i = 0; i < m; ++i) { if (c[i] == 2) { dp[S] = min(dp[S], 1 + max(dfs(S - pow3[i]), dfs(S - 2 * pow3[i]))); } } return dp[S]; } } int main() { for (int i = 1; i < 12; ++i) pow3[i] = pow3[i - 1] * 3; ios_base::sync_with_stdio(false); cin.tie(0); while (cin >> m >> n and n) { vector<int> A; for (int i = 0; i < n; ++i) { string s; cin >> s; int ans = 0; for (int i = 0; i < m; ++i) { ans |= (s[i] - '0')<<i; } A.emplace_back(ans); } fill(dp, dp + pow3[m], -1); for (int S = 0; S < pow3[m]; ++S) { vector<int> c = decode(S); cnt[S] = 0; for (int i = 0; i < n; ++i) { bool test = true; for (int j = 0; j < m and test; ++j) { test &= c[j] == 2 or c[j] == ((A[i]>>j)&1); } cnt[S] += test; } } cout << dfs(pow3[m] - 1) << '\n'; } }
CPP
p01012 Planarian Regeneration
Notes For this problem, it is recommended to use floating point numbers, which are more accurate than double. Input m n x k l y For input, six integers m, n, x, k, l, y are given in the above input format. These six integers correspond to those in the problem statement, repeating the horizontal m: n cut x times and the vertical k: l cut y times. 1 ≤ m, n, k, l ≤ 100, 0 ≤ x, y ≤ 40, and m, n and l, k are relatively prime. Output Print out the expected number of planarians that are regenerating to their original form after a few weeks in a row. The output is acceptable with an error of 10-6 or less. Examples Input 1 1 1 1 1 1 Output 0.562500 Input 1 2 2 1 1 1 Output 0.490741 Input 1 2 0 3 4 0 Output 1.000000
7
0
#include <bits/stdc++.h> using namespace std; int cnt[45][45] = {0}; long double dis[45][45] = {0}; long double sum[45][45] = {0}; int m, n, x; long double ans = 1; long double solve(); int main() { cout << fixed << setprecision(12); int t; for(t = 0; t < 2; ++t) { cin >> m >> n >> x; ans *= solve(); for(int i = 0; i < 44; ++i) for(int j = 0; j < 44; ++j) cnt[i][j] = dis[i][j] = sum[i][j] = 0; } cout << ans << endl; return 0; } long double solve() { long double nans = 0; int i, j; long double mn = (long double)(m + n); cnt[0][0] = sum[0][0] = 1; for(i = 0; i < 44; ++i) dis[0][i] = 1; for(i = 1; i <= x; ++i) { for(j = 0; j < i; ++j) { cnt[i][j] += cnt[i - 1][j]; cnt[i][j + 1] += cnt[i - 1][j]; } for(j = 0; j <= x; ++j) { if(j < i) dis[i][j] = dis[i - 1][j] * (long double)m / mn; else dis[i][j] = dis[i - 1][j] * (long double)n / mn; } for(j = 0; j < i; ++j) { sum[i][j] += sum[i - 1][j]; sum[i][j + 1] += sum[i - 1][j] - dis[i][j] * (long double)cnt[i - 1][j]; } } for(i = 0; i <= x; ++i) nans += sum[x][i] * dis[x][i]; return nans; }
CPP
p01012 Planarian Regeneration
Notes For this problem, it is recommended to use floating point numbers, which are more accurate than double. Input m n x k l y For input, six integers m, n, x, k, l, y are given in the above input format. These six integers correspond to those in the problem statement, repeating the horizontal m: n cut x times and the vertical k: l cut y times. 1 ≤ m, n, k, l ≤ 100, 0 ≤ x, y ≤ 40, and m, n and l, k are relatively prime. Output Print out the expected number of planarians that are regenerating to their original form after a few weeks in a row. The output is acceptable with an error of 10-6 or less. Examples Input 1 1 1 1 1 1 Output 0.562500 Input 1 2 2 1 1 1 Output 0.490741 Input 1 2 0 3 4 0 Output 1.000000
7
0
#include "bits/stdc++.h" #include<unordered_map> #include<unordered_set> #pragma warning(disable:4996) using namespace std; using ld = long double; const ld eps = 1e-7; //// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt" ld getans(ld l,ld r, int b,ld c) { if (b == 0||abs(r-l)<eps) { return (1-l)*(r - l); } ld aa = getans(l,(1-c)*l+c*r, b - 1, c); ld ab = getans((1 - c)*l + c*r,r, b - 1, c); return aa + ab; } ld get() { int m, n, x; cin >> m >> n >> x; ld c = (ld(m)) / (m + n); x = min(x, 40); return getans(0,1, x, c); } int main() { ld a = get(); ld b = get(); cout << setprecision(10) << fixed << a*b << endl; return 0; }
CPP
p01012 Planarian Regeneration
Notes For this problem, it is recommended to use floating point numbers, which are more accurate than double. Input m n x k l y For input, six integers m, n, x, k, l, y are given in the above input format. These six integers correspond to those in the problem statement, repeating the horizontal m: n cut x times and the vertical k: l cut y times. 1 ≤ m, n, k, l ≤ 100, 0 ≤ x, y ≤ 40, and m, n and l, k are relatively prime. Output Print out the expected number of planarians that are regenerating to their original form after a few weeks in a row. The output is acceptable with an error of 10-6 or less. Examples Input 1 1 1 1 1 1 Output 0.562500 Input 1 2 2 1 1 1 Output 0.490741 Input 1 2 0 3 4 0 Output 1.000000
7
0
#include <iostream> #include <sstream> #include <cstdio> #include <cstdlib> #include <cmath> #include <ctime> #include <cstring> #include <string> #include <vector> #include <stack> #include <queue> #include <deque> #include <map> #include <set> #include <bitset> #include <numeric> #include <utility> #include <iomanip> #include <algorithm> #include <functional> using namespace std; typedef long long ll; typedef vector<int> vint; typedef vector<long long> vll; typedef pair<int,int> pint; typedef pair<long long, long long> pll; #define MP make_pair #define PB push_back #define ALL(s) (s).begin(),(s).end() #define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } template<class T> ostream& operator << (ostream &s, vector<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template<class T> ostream& operator << (ostream &s, vector<vector<T> > P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; } const int MAX_C = 110; long double Com[MAX_C][MAX_C]; void calc_com() { for (int i = 0; i < MAX_C; ++i) for (int j = 0; j < MAX_C; ++j) Com[i][j] = 0; Com[0][0] = 1; for (int i = 1; i < MAX_C; ++i) { Com[i][0] = 1; for (int j = 1; j < MAX_C; ++j) { Com[i][j] = Com[i-1][j-1] + Com[i-1][j]; } } } long double vpm[210], vpn[210], vpk[210], vpl[210]; int m, n, x, k, l, y; int main() { //freopen( "/Users/macuser/Dropbox/Contest/input.in", "r", stdin ); calc_com(); while (cin >> m >> n >> x >> k >> l >> y) { long double pm = (long double)(m)/(m+n); long double pk = (long double)(k)/(k+l); vpm[0] = 1, vpn[0] = 1, vpk[0] = 1, vpl[0] = 1; for (int i = 1; i < 210; ++i) { vpm[i] = vpm[i-1] * pm; vpn[i] = vpn[i-1] * (1.0 - pm); vpk[i] = vpk[i-1] * pk; vpl[i] = vpl[i-1] * (1.0 - pk); } long double d1 = 0, d2 = 0; for (int i = x-1; i >= 0; --i) { long double tmp = 0; for (int j = 0; j <= i; ++j) { long double f = vpm[(i-j)*2+1] * vpn[j*2+1]; f *= Com[i][j]; tmp += f; //cout << i << ", " << j << " : " << f << endl; } d1 += tmp; } for (int i = y-1; i >= 0; --i) { long double tmp = 0; for (int j = 0; j <= i; ++j) { long double f = vpk[(i-j)*2+1] * vpl[j*2+1]; f *= Com[i][j]; tmp += f; //cout << i << ", " << j << " : " << f << endl; } d2 += tmp; } long double res = (long double)(1.0) - d1 - d2 + d1 * d2; cout << fixed << setprecision(9) << res << endl; } return 0; }
CPP
p01012 Planarian Regeneration
Notes For this problem, it is recommended to use floating point numbers, which are more accurate than double. Input m n x k l y For input, six integers m, n, x, k, l, y are given in the above input format. These six integers correspond to those in the problem statement, repeating the horizontal m: n cut x times and the vertical k: l cut y times. 1 ≤ m, n, k, l ≤ 100, 0 ≤ x, y ≤ 40, and m, n and l, k are relatively prime. Output Print out the expected number of planarians that are regenerating to their original form after a few weeks in a row. The output is acceptable with an error of 10-6 or less. Examples Input 1 1 1 1 1 1 Output 0.562500 Input 1 2 2 1 1 1 Output 0.490741 Input 1 2 0 3 4 0 Output 1.000000
7
0
#include <bits/stdc++.h> #define REP(i,n) for(int i=0; i<(int)(n); ++i) using namespace std; typedef long double ldouble; ldouble calc(int m, int n, int x) { // n / (m + n), m / (m + n) ldouble len[50][50] = {}; len[0][0] = 1.0L; for(int i = 0; i <= x; i++) { for(int j = 0; j <= x; j++) { len[i + 1][j] = len[i][j] * n / (m + n); len[i][j + 1] = len[i][j] * m / (m + n); } } long long cnt[50][50] = {}; cnt[0][0] = 1; for(int i = 0; i <= x; i++){ for(int j = 0; j <= x; j++){ if(i > 0) cnt[i][j] += cnt[i - 1][j]; if(j > 0) cnt[i][j] += cnt[i][j - 1]; // printf("cnt[%d][%d] = %lld\n", i, j, cnt[i][j]); } } ldouble dist[50][50] = {}; dist[0][0] = 0.0; for(int i = 0; i <= x; i++){ for(int j = 0; j <= x; j++) { dist[i + 1][j] += dist[i][j]; dist[i][j + 1] += dist[i][j] + len[i + 1][j] * cnt[i][j]; // printf("dist[%d][%d] = %Lf\n", i, j, dist[i][j]); } } ldouble res = 0; for(int i = 0; i <= x; i++) { int j = x - i; // printf("i = %d : (%Lf + %Lf) * %Lf = %Lf\n", i, dist[i][j], len[i][j], len[i][j], (dist[i][j] + len[i][j]) * len[i][j]); res += (dist[i][j] + len[i][j] * cnt[i][j]) * len[i][j]; } return res; } int main(){ int m, n, x; int k, l, y; cin >> m >> n >> x; cin >> k >> l >> y; ldouble xsum = calc(m, n, x); ldouble ysum = calc(k, l, y); printf("%.12Lf\n", xsum * ysum); return 0; }
CPP
p01012 Planarian Regeneration
Notes For this problem, it is recommended to use floating point numbers, which are more accurate than double. Input m n x k l y For input, six integers m, n, x, k, l, y are given in the above input format. These six integers correspond to those in the problem statement, repeating the horizontal m: n cut x times and the vertical k: l cut y times. 1 ≤ m, n, k, l ≤ 100, 0 ≤ x, y ≤ 40, and m, n and l, k are relatively prime. Output Print out the expected number of planarians that are regenerating to their original form after a few weeks in a row. The output is acceptable with an error of 10-6 or less. Examples Input 1 1 1 1 1 1 Output 0.562500 Input 1 2 2 1 1 1 Output 0.490741 Input 1 2 0 3 4 0 Output 1.000000
7
0
#include "bits/stdc++.h" using namespace std; using ld = long double; const ld eps = 1e-7; ld getans(ld l,ld r, int b,ld c) { if (b == 0||abs(r-l)<eps) { return (1-l)*(r - l); } ld aa = getans(l,(1-c)*l+c*r, b - 1, c); ld ab = getans((1 - c)*l + c*r,r, b - 1, c); return aa + ab; } ld get() { int m, n, x; cin >> m >> n >> x; ld c = (ld(m)) / (m + n); return getans(0,1, x, c); } int main() { cout << setprecision(10) << fixed << get()*get() << endl; return 0; }
CPP
p01012 Planarian Regeneration
Notes For this problem, it is recommended to use floating point numbers, which are more accurate than double. Input m n x k l y For input, six integers m, n, x, k, l, y are given in the above input format. These six integers correspond to those in the problem statement, repeating the horizontal m: n cut x times and the vertical k: l cut y times. 1 ≤ m, n, k, l ≤ 100, 0 ≤ x, y ≤ 40, and m, n and l, k are relatively prime. Output Print out the expected number of planarians that are regenerating to their original form after a few weeks in a row. The output is acceptable with an error of 10-6 or less. Examples Input 1 1 1 1 1 1 Output 0.562500 Input 1 2 2 1 1 1 Output 0.490741 Input 1 2 0 3 4 0 Output 1.000000
7
0
#include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <climits> #include <ctime> #include <queue> #include <stack> #include <algorithm> #include <list> #include <vector> #include <set> #include <map> #include <iostream> #include <deque> #include <complex> #include <string> #include <iomanip> #include <sstream> #include <bitset> #include <valarray> #include <unordered_map> #include <iterator> #include <functional> #include <cassert> using namespace std; typedef long long int ll; typedef unsigned int uint; typedef unsigned char uchar; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; #define REP(i,x) for(int i=0;i<(int)(x);i++) #define REPS(i,x) for(int i=1;i<=(int)(x);i++) #define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--) #define RREPS(i,x) for(int i=((int)(x));i>0;i--) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++) #define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++) #define ALL(container) (container).begin(), (container).end() #define RALL(container) (container).rbegin(), (container).rend() #define SZ(container) ((int)container.size()) #define mp(a,b) make_pair(a, b) #define pb push_back #define eb emplace_back #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } template<class T> ostream& operator<<(ostream &os, const vector<T> &t) { os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os; } template<class T> ostream& operator<<(ostream &os, const set<T> &t) { os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os; } template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";} template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);} template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);} const int INF = 1<<28; const double EPS = 1e-8; const int MOD = 1000000007; typedef long double R; R m, n, x; template<int N> struct Comb{ R C[N][N]; Comb(){ memset(C, 0, (int)sizeof(C)); for(int i = 0; i < 50; i++){ C[i][0] = C[i][i] = 1; for(int j = 1; j < i; j++) C[i][j] = C[i-1][j-1] + C[i-1][j]; } } R operator()(int n, int r){ if(n < 0 || r < 0 || r > n) return 0; return C[n][r]; } }; Comb<50> C; R solve(R l, R r, R x){ R res = 0; REP(i, x+1){ // i回左に行く R sum = C(x, i); REPS(j, x){ // j回目の遷移 REP(k, j){ // 既に j - 1 回中 k 回左に遷移している R w = pow(l, k) * pow(r, j-1-k); sum -= w*(l) * C(j-1, k) * C(x-j, i-k ); // j回目に右に行く } } res += pow(l, i) * pow(r, x-i) * sum; } return res; } int main(int argc, char *argv[]){ ios::sync_with_stdio(false); R ans = 1; REP(i, 2){ cin >> m >> n >> x; ans *= solve(m/(n+m), n/(n+m), x); } printf("%.11f\n", (double)ans); return 0; }
CPP
p01012 Planarian Regeneration
Notes For this problem, it is recommended to use floating point numbers, which are more accurate than double. Input m n x k l y For input, six integers m, n, x, k, l, y are given in the above input format. These six integers correspond to those in the problem statement, repeating the horizontal m: n cut x times and the vertical k: l cut y times. 1 ≤ m, n, k, l ≤ 100, 0 ≤ x, y ≤ 40, and m, n and l, k are relatively prime. Output Print out the expected number of planarians that are regenerating to their original form after a few weeks in a row. The output is acceptable with an error of 10-6 or less. Examples Input 1 1 1 1 1 1 Output 0.562500 Input 1 2 2 1 1 1 Output 0.490741 Input 1 2 0 3 4 0 Output 1.000000
7
0
#include <cstdio> #include <queue> #include <algorithm> #define rep(i, n) for(int i = 0; i < (n); ++i) using namespace std; typedef long double ld; int m, n, x, k, l, y; ld side(ld a, int k){ if(k == 0){ return 1; } return (2 * a * a - 2 * a + 1) * side(a, k - 1) + a * (1 - a); } int main(){ scanf("%d%d%d%d%d%d", &m, &n, &x, &k, &l, &y); printf("%Lf\n", side(n / ld(n + m), x) * side(l / ld(l + k), y)); return 0; }
CPP
p01012 Planarian Regeneration
Notes For this problem, it is recommended to use floating point numbers, which are more accurate than double. Input m n x k l y For input, six integers m, n, x, k, l, y are given in the above input format. These six integers correspond to those in the problem statement, repeating the horizontal m: n cut x times and the vertical k: l cut y times. 1 ≤ m, n, k, l ≤ 100, 0 ≤ x, y ≤ 40, and m, n and l, k are relatively prime. Output Print out the expected number of planarians that are regenerating to their original form after a few weeks in a row. The output is acceptable with an error of 10-6 or less. Examples Input 1 1 1 1 1 1 Output 0.562500 Input 1 2 2 1 1 1 Output 0.490741 Input 1 2 0 3 4 0 Output 1.000000
7
0
#include <bits/stdc++.h> using namespace std; using ll = long long; #define repl(i,a,b) for(int (i)=(int)a;(i)<(int)(b);++(i)) #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define all(x) (x).begin(),(x).end() #define pb push_back #define fi first #define se second #define dbg(x) cerr<<#x<<"="<<x<<endl long double cnt[44][44]; long double dp[44][44]; long double calc(int m,int n,int x){ long double p1=(long double)m/(long double)(m+n); long double p2=(long double)n/(long double)(m+n); rep(i,44)rep(j,44)dp[i][j]=cnt[i][j]=0; cnt[0][0]=1; rep(h,x){ rep(l,h+1){ int r=h-l; long double d=pow(p1,l)*pow(p2,r); { cnt[l+1][r]+=cnt[l][r]; dp[l+1][r]+=dp[l][r]; } { cnt[l][r+1]+=cnt[l][r]; dp[l][r+1]+=dp[l][r]+d*p1*cnt[l][r]; } } } long double res=0; rep(l,x+1){ int r=x-l; res+=pow(p1,l)*pow(p2,r)*(dp[l][r]+cnt[l][r]*pow(p1,l)*pow(p2,r)); } return res; } int main(){ int M,N,X,K,L,Y; cin>>M>>N>>X>>K>>L>>Y; long double a=calc(N,M,X); long double b=calc(L,K,Y); printf("%.10Lf\n",a*b); return 0; }
CPP
p01012 Planarian Regeneration
Notes For this problem, it is recommended to use floating point numbers, which are more accurate than double. Input m n x k l y For input, six integers m, n, x, k, l, y are given in the above input format. These six integers correspond to those in the problem statement, repeating the horizontal m: n cut x times and the vertical k: l cut y times. 1 ≤ m, n, k, l ≤ 100, 0 ≤ x, y ≤ 40, and m, n and l, k are relatively prime. Output Print out the expected number of planarians that are regenerating to their original form after a few weeks in a row. The output is acceptable with an error of 10-6 or less. Examples Input 1 1 1 1 1 1 Output 0.562500 Input 1 2 2 1 1 1 Output 0.490741 Input 1 2 0 3 4 0 Output 1.000000
7
0
m,n,x=[int(i) for i in input().split()] k,l,y=[int(i) for i in input().split()] res=0.5*(1.0+((m**2+n**2)**x)/((m+n)**(2*x))) res*=0.5*(1.0+((k**2+l**2)**y)/((k+l)**(2*y))) print(res)
PYTHON3
p01012 Planarian Regeneration
Notes For this problem, it is recommended to use floating point numbers, which are more accurate than double. Input m n x k l y For input, six integers m, n, x, k, l, y are given in the above input format. These six integers correspond to those in the problem statement, repeating the horizontal m: n cut x times and the vertical k: l cut y times. 1 ≤ m, n, k, l ≤ 100, 0 ≤ x, y ≤ 40, and m, n and l, k are relatively prime. Output Print out the expected number of planarians that are regenerating to their original form after a few weeks in a row. The output is acceptable with an error of 10-6 or less. Examples Input 1 1 1 1 1 1 Output 0.562500 Input 1 2 2 1 1 1 Output 0.490741 Input 1 2 0 3 4 0 Output 1.000000
7
0
#include <bits/stdc++.h> using namespace std; #define _MACRO(_1, _2, _3, NAME, ...) NAME #define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define _rep(i,n) _repl(i,0,n) #define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__) #define pb push_back #define all(x) begin(x),end(x) #define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x)) #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) void _dbg(string){cerr<<endl;} template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);} template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;} template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;} #else #define dbg(...) {} #endif using LD = long double; LD mypow(LD p, LD r){ LD ans = 1; rep(i,r) ans *= p; return ans; } LD calc(LD p, LD q, LD r){ LD tmp = mypow((LD)p/(p+q), 2*r); // p^r / (p+q)^r LD ans = tmp; LD comb = 1; rep(i,r){ comb = comb * (r-i) / (i+1); dbg(comb); tmp = tmp / p / p * q * q; ans += comb * tmp; } return (1.0 + ans) / 2.0; } int main(){ LD m,n,x; LD k,l,y; scanf("%Lf %Lf %Lf %Lf %Lf %Lf", &m, &n, &x, &k, &l, &y); printf("%.7Lf\n", calc(m,n,x) * calc(k,l,y)); return 0; }
CPP
p01012 Planarian Regeneration
Notes For this problem, it is recommended to use floating point numbers, which are more accurate than double. Input m n x k l y For input, six integers m, n, x, k, l, y are given in the above input format. These six integers correspond to those in the problem statement, repeating the horizontal m: n cut x times and the vertical k: l cut y times. 1 ≤ m, n, k, l ≤ 100, 0 ≤ x, y ≤ 40, and m, n and l, k are relatively prime. Output Print out the expected number of planarians that are regenerating to their original form after a few weeks in a row. The output is acceptable with an error of 10-6 or less. Examples Input 1 1 1 1 1 1 Output 0.562500 Input 1 2 2 1 1 1 Output 0.490741 Input 1 2 0 3 4 0 Output 1.000000
7
0
#include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <climits> #include <ctime> #include <queue> #include <stack> #include <algorithm> #include <list> #include <vector> #include <set> #include <map> #include <iostream> #include <deque> #include <complex> #include <string> #include <iomanip> #include <sstream> #include <bitset> #include <valarray> #include <unordered_map> #include <iterator> #include <functional> #include <cassert> using namespace std; typedef long long int ll; typedef unsigned int uint; typedef unsigned char uchar; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; #define REP(i,x) for(int i=0;i<(int)(x);i++) #define REPS(i,x) for(int i=1;i<=(int)(x);i++) #define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--) #define RREPS(i,x) for(int i=((int)(x));i>0;i--) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++) #define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++) #define ALL(container) (container).begin(), (container).end() #define RALL(container) (container).rbegin(), (container).rend() #define SZ(container) ((int)container.size()) #define mp(a,b) make_pair(a, b) #define pb push_back #define eb emplace_back #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } template<class T> ostream& operator<<(ostream &os, const vector<T> &t) { os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os; } template<class T> ostream& operator<<(ostream &os, const set<T> &t) { os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os; } template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";} template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);} template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);} const int INF = 1<<28; const double EPS = 1e-8; const int MOD = 1000000007; typedef long double R; R m, n; int x; template<int N> struct Comb{ R C[N][N]; Comb(){ memset(C, 0, (int)sizeof(C)); for(int i = 0; i < 50; i++){ C[i][0] = C[i][i] = 1; for(int j = 1; j < i; j++) C[i][j] = C[i-1][j-1] + C[i-1][j]; } } R operator()(int n, int r){ if(n < 0 || r < 0 || r > n) return 0; return C[n][r]; } }; Comb<50> C; R powL[51], powR[51]; R solve(R l, R r, int x){ powL[0] = powR[0] = 1; REPS(i, x){ powL[i] = powL[i-1] * l; powR[i] = powR[i-1] * r; } R res = 0; REP(i, x+1){ // i回左に行く R sum = C(x, i); REPS(j, x){ // j回目の遷移 REP(k, j){ // 既に j - 1 回中 k 回左に遷移している R w = powL[k] * powR[j-1-k]; sum -= w*(l) * C(j-1, k) * C(x-j, i-k ); // j回目に右に行く } } res += powL[i] * powR[x-i] * sum; } return res; } int main(int argc, char *argv[]){ ios::sync_with_stdio(false); R ans = 1; REP(i, 2){ cin >> m >> n >> x; ans *= solve(m/(n+m), n/(n+m), x); } printf("%.11f\n", (double)ans); return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while True: N,M = map(int,input().strip().split(" ")) if [N,M] == [0,0]: break L = [] S = 0 for i in range(N): l = list(map(int,input().strip().split(" "))) l.reverse() L.append(l) S = S + l[0]*l[1] L.sort() for j in range(0,N): t = N-j-1 if M >= L[t][1]: S = S - L[t][0]*L[t][1] M = M - L[t][1] elif 0 < M: S = S - L[t][0]*M M = M - L[t][1] print(S)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <functional> #include <vector> #include <algorithm> using namespace std; int main(){ int n, m; while (cin >> n >> m, n){ vector<pair<int, int> > dp; for (int i = 0; i < n; i++){ int d, p; cin >> d >> p; dp.push_back(make_pair(p, d)); } sort(dp.begin(), dp.end(), greater<pair<int, int> >()); int ret = 0; for (int unsigned i = 0; i < dp.size(); i++){ for (int j = 0; j < dp[i].second; j++){ if (m >0){ m--; } else { ret += dp[i].first; } } } cout << ret << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<iostream> int n,m,i,d,p,z; int main(){ while(std::cin>>n>>m,n){ int x[11]={}; for(z=i=0;i++<n;x[p]+=d,z+=d*p)std::cin>>d>>p; for(i=10;i>=0;--i) if(x[i]<m)z-=x[i]*i,m-=x[i]; else if(x[i])z-=i*m,m=0; std::cout<<z<<'\n'; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <algorithm> #include <utility> using namespace std; int main(){ while(1){ int N,M; long ans=0; cin >> N >> M; pair<int,int> X[N]; if(N==0&&M==0) break; for(int i=0;i<N;++i) cin >> X[i].second >> X[i].first; sort(X,X+N); reverse(X,X+N); for(int i=0;i<N&&M>0;++i){ for(;X[i].second>0&&M>0;--M){ X[i].second--; } } for(int i=0;i<N;++i){ ans+=X[i].first*X[i].second; } cout<<ans<<endl;; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.util.*; public class Main{ class C implements Comparable<C>{ int d,p; public C(int d, int p) { this.d = d; this.p = p; } @Override public int compareTo(C o) { if(this.p < o.p) return 1; if(this.p > o.p) return -1; return 0; } } private void doit(){ Scanner sc = new Scanner(System.in); while(true){ int n = sc.nextInt(); int m = sc.nextInt(); if((n|m) == 0) break; C [] data = new C[n]; for(int i = 0; i < n; i++){ int d = sc.nextInt(); int p = sc.nextInt(); data[i] = new C(d, p); } Arrays.sort(data); int ind = 0; while(true){ int res = m - data[ind].d; if(res >= 0){ m = res; data[ind].d = 0; ind++; if(ind == n) break; } else { data[ind].d = data[ind].d - m; break; } } int sum = 0; for(int i = 0; i < n; i++){ sum += data[i].d * data[i].p; } System.out.println(sum); } } public static void main(String[] args) { new Main().doit(); } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.util.*; public class Main { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); while (true) { long n = stdIn.nextLong(); long m = stdIn.nextLong(); if(n == 0 && m == 0) break; Long[] c = new Long[11]; for (int i = 0; i < 11; i++) { c[i] = 0L; } for (int i = 0; i < n; i++) { long d = stdIn.nextLong(); int p = stdIn.nextInt(); c[p] += d; } for (int i = 10; i >= 0; i--) { c[i] -= m; m = 0; if (c[i] < 0) { m += c[i] * -1L; c[i] = 0L; } if (m == 0) break; } Long ans = 0L; for (int i = 0; i <= 10; i++) { ans += c[i] * i; } System.out.println(ans); } } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <algorithm> using namespace std; int N,M; pair<int,int> PD[10010]; int main() { while(cin >>N >> M && N) { int d,p; for(int i = 0;i<N;i++) { cin >> d; cin >> p; PD[i] = make_pair(p,d); } sort(PD,PD+N); int S=0; for(int i=0;i<N;i++) S += PD[i].first*PD[i].second; for(int i=N-1;i>=0;i--) { if(M <=0) break; int guarded = min(M,PD[i].second); S-=PD[i].first*guarded; M-=guarded; } cout << S << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <algorithm> using namespace std; int main(){ int N, M; while(cin >> N >> M && N){ int d, p, dp[11]; for(int i=0; i<11; ++i) dp[i] = 0; for(int i=0; i<N; ++i){ cin >> d >> p; dp[p] += d; } int ex = 0; for(int i=10;i>0;--i){ if(M >= dp[i]) M -= dp[i]; else{ ex += (dp[i] - M) * i; M = 0; } } cout << ex << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
public class Main{ public void run(java.io.InputStream in, java.io.PrintStream out){ java.util.Scanner sc = new java.util.Scanner(in); int n, m, i, j, tmp, sum; int[] d, p; for(;;){ n = sc.nextInt(); m = sc.nextInt(); if(n == 0 && m == 0)break; d = new int[n]; p = new int[n]; for(i = 0;i < n;i++){ d[i] = sc.nextInt(); p[i] = sc.nextInt(); } for(i = 0;i < n - 1;i++)for(j = 0;j < n - 1 - i;j++)if(p[j] < p[j + 1]){ tmp = d[j]; d[j] = d[j + 1]; d[j + 1] = tmp; tmp = p[j]; p[j] = p[j + 1]; p[j + 1] = tmp; } for(i = 0;i < n;i++){ if(m >= d[i]){ m -= d[i]; d[i] = 0; }else{ d[i] -= m; m = 0; break; } } if(m > 0)out.println("0"); else{ for(sum = 0;i < n;i++)sum += d[i] * p[i]; out.println(sum); } } sc.close(); } public static void main(String[] args){ (new Main()).run(System.in, System.out); } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import sys while True: n,m = [int(i) for i in input().split()] if n == 0 and m == 0: sys.exit() dp = [] ans = 0 for i in range(n): dp.append([int(j) for j in input().split()]) dp.sort(key=lambda x:x[1],reverse=True) for i in range(n): if dp[i][0] <= m: m -= dp[i][0] elif m > 0: ans += dp[i][1] * (dp[i][0] - m) m = 0 else: ans += dp[i][1] * dp[i][0] print(ans)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.util.*; import java.io.*; import java.awt.geom.*; import java.math.*; public class Main { static final Scanner in = new Scanner(System.in); static final PrintWriter out = new PrintWriter(System.out,false); static boolean debug = false; static boolean solve() { int n = in.nextInt(); int m = in.nextInt(); if (n + m == 0) return false; int[][] p = new int[n][2]; for (int i=0; i<n; i++) { for (int j=0; j<2; j++) { p[i][j] = in.nextInt(); } } Arrays.sort(p, new Comparator<int[]>(){ public int compare(int[] a, int[] b) { return b[1] - a[1]; } }); int ans = 0; for (int i=0; i<n; i++) { int min = Math.min(Math.max(0, m), p[i][0]); ans += (p[i][0] - min)*p[i][1]; m -= min; } out.println(ans); return true; } public static void main(String[] args) { debug = args.length > 0; long start = System.nanoTime(); while(solve()); out.flush(); long end = System.nanoTime(); dump((end - start) / 1000000 + " ms"); in.close(); out.close(); } static void dump(Object... o) { if (debug) System.err.println(Arrays.deepToString(o)); } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <queue> #include <sstream> using namespace std; int main(){ while(true){ long long int N, M; cin >> N >> M; if(N == 0 && M == 0){ break; } long long int a[20]; for(int i = 0; i < 20; i++){ a[i] = 0; } for(int i = 0; i < N; i++){ int num1, num2; cin >> num1 >> num2; a[num2] += num1; } long long int ans = 0; for(int i = 10; i >= 0; i--){ long long int ret = max(0LL, a[i] - M); ans += ret * i; M = max(0LL, M - a[i]); } cout << ans << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <map> #define rep(i,n) for(int i=0;i<(int)(n);i++) using namespace std; int main(void) { long n, m; while(cin >> n >> m, n > 0){ map<long, long> pd; long ans=0; //map pdを構成 rep(i, n){ int d, p; cin >> d >> p; if (pd.count(p) == 0){ pd.insert(make_pair(p, d)); }else{ pd[p] += d; } } for(auto itr = pd.rbegin(); itr != pd.rend(); itr++){ if (m > itr->second){ m -= itr->second; }else{ ans += itr->first * (itr->second - m); m = 0; } } cout << ans << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while True: n,m=map(int,input().split()) matrix=[] if n==m==0: break else: for i in range(n): D,P=map(int,input().split()) matrix.append([D,P]) matrix.sort(key = lambda x:x[1],reverse=True) i=0 while m>0 and i<=n-1: pp=matrix[i][1] dd=matrix[i][0] if dd > m: matrix[i][0]=dd-m m=0 else: matrix[i][1]=0 m=m-dd i+=1 sum_matrix=[mm[0]*mm[1] for mm in matrix] print(sum(sum_matrix))
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> using namespace std; int main(){ int n,m; while(cin>>n>>m,n){ int a[11]={0}; for(int i=0;i<n;i++){ int b,c; cin>>b>>c; a[c]+=b; } int ans=0; for(int i=10;i>=0;i--){ if(m-a[i]>=0) m-=a[i]; else{ ans+=i*(a[i]-m); m=0; } } cout<<ans<<endl; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #include<vector> #include<cstring> #include<sstream> #include<iomanip> using namespace std; #define INF 999999999 int main(){ int ans, i, j, k, n, m, d, p, dis; int at[11]; while(1){ cin >> n >> m; if(!n) break; for(k=0;k<=10;k++)at[k] = 0; ans = 0; dis = 0; for(i=0;i<n;i++){ cin >> d >> p; at[p] += d; dis += d; } dis -= m; for(j=0;j<=10&&dis>0;j++){ if(at[j] <= dis){ dis -= at[j]; ans += at[j] * j; } else{ ans += dis * j; dis = 0; } } cout << ans << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.util.Scanner; public class Main { static int calcExpectation(int[][] memo, int dis, int money) { for(int r = 0; r < memo.length; r++) { int max = memo[0][1]; int minusDis = memo[0][0], idx = 0; for(int c = 1; c < memo.length; c++) { if(memo[c][1] > max) { max = memo[c][1]; minusDis = memo[c][0]; idx = c; } } if(money >= minusDis) { money -= minusDis; memo[idx][0] = 0; memo[idx][1] = 0; } else { memo[idx][0] -= money; money = 0; } if(money == 0) break; } int ret = 0; for(int r = 0; r < memo.length; r++) { int num = memo[r][0] * memo[r][1]; if(num > 0) ret += num; } return(ret); } public static void main(String[] args) { // TODO Auto-generated method stub Scanner stdIn = new Scanner(System.in); while(true) { int n = stdIn.nextInt(); int m = stdIn.nextInt(); if(n + m == 0) break; int[][] memo = new int[n][2]; int sumDistance = 0; for(int r = 0; r < n; r++) { memo[r][0] = stdIn.nextInt(); //距離 memo[r][1] = stdIn.nextInt(); //期待値 sumDistance += memo[r][0]; } System.out.println(calcExpectation(memo, sumDistance, m)); } } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#! /usr/bin/env python # -*- coding: utf-8 -*- import os import sys import itertools import math from collections import Counter, defaultdict class Main(object): def __init__(self): pass def solve(self): ''' insert your code ''' while True: n, m = map(int, raw_input().split()) if n == 0 and m == 0: break l = [] for i in range(n): d, p = map(int, raw_input().split()) l.append((p, d)) l.sort(reverse=True) rest = m prob = 0 for p, d in l: rest -= d if rest < 0: prob += -1 * rest * p rest = 0 print prob return None if __name__ == '__main__': m = Main() m.solve()
PYTHON
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <algorithm> using namespace std; int main(){ int N, M, D[10005], P[10005]; while( cin >> N >> M, N|M ){ for(int i=0; i<N; i++){ cin >> D[i] >> P[i]; } for(int i=0; i<N; i++){ for(int j=1; j<N; j++){ if( P[j-1] < P[j] ){ int tmp = P[j-1]; P[j-1] = P[j]; P[j] = tmp; tmp = D[j-1]; D[j-1] = D[j]; D[j] = tmp; } } } int ans=0; for(int i=0; i<N; i++){ if( M>0 ){ ans += max(0, D[i]-M) * P[i]; M -= D[i]; }else{ ans += D[i] * P[i]; } } cout << ans << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); int m = sc.nextInt(); if (n == 0 && m == 0) break; int[] d = new int[n]; int[] p = new int[n]; for (int i = 0; i < n; i++) { d[i] = sc.nextInt(); p[i] = sc.nextInt(); } int[] pr = new int[n]; for (int i = 0; i < n; i++) pr[i] = p[i]; Arrays.sort(pr); for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < n; j++) { if (pr[i] == p[j]) { if (d[j] == 0) continue; if (d[j] < m) { m -= d[j]; d[j] = 0; break; } if (d[j] >= m) { d[j] -= m; m = 0; break; } } } if (m == 0) break; } int result = 0; for (int i = 0; i < n; i++) result += p[i] * d[i]; System.out.println(result); } sc.close(); } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<iostream> #include<algorithm> #include<utility> using namespace std; int N,M; int S; pair<int,int>PD[10010]; int main() { while(cin>>N>>M&&N) { int d,p; for(int i=0;i<N;i++) { cin>>d>>p; PD[i]=make_pair(p,d); } sort(PD,PD+N); S=0; for(int j=0;j<N;j++) { S+=PD[j].first*PD[j].second; } for(int i=N-1;i>=0;i--) { if(M<=0) break; int guarded=min(M,PD[i].second); S-=PD[i].first*guarded; M-=guarded; } cout<<S<<endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <algorithm> #include <string> #include <utility> #include <vector> using namespace std; int n,m; int main() { while(cin >> n >> m && n > 0) { pair<int, int> pd [10000]; int d,p; int y = 0; for(int i = 0; i < n; i++) { cin >> d >> p; pd[i] = make_pair(p, d); y += d*p; } sort(pd,pd+n); reverse(pd,pd+n); for(int i = 0; i < n; i++) { if(m < 0) cout << y; else{ int e = min(m, pd[i].second); y -= pd[i].first * e; m -= e; } } cout << y << endl; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main { /** * @param args */ public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(true){ int N = sc.nextInt(); int M = sc.nextInt(); if(N+M==0)break; int[][]p =new int[N][2]; long ans=0; for (int i = 0; i < N; i++) { p[i][0]=sc.nextInt(); p[i][1]=sc.nextInt(); ans+=(long)p[i][0]*p[i][1]; } Arrays.sort(p,new Comparator<int[]>(){ @Override public int compare(int[] arg0, int[] arg1) { return arg1[1]-arg0[1]; } }); for (int i = 0; i < p.length; i++) { ans = ans - p[i][1]*Math.min(p[i][0], M); M = M - p[i][0]; if(M<=0)break; } System.out.println(ans); } } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<bits/stdc++.h> int d[11],n,m,i,a,b,s,e; int main(){ while(true) { memset(d,0,sizeof(d));s=0; std::cin>>n>>m;if(!n){break;} for(i=0;i<n;i++){std::cin>>a>>b;d[b]+=a;} for(i=10;i>=0;i--){e=std::min(d[i],m);m-=e;s+=i*(d[i]-e);} printf("%d\n",s);} }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while True: n, m = map(int, input().split()) if n == 0: break else: s = 0 l = [] for _ in range(n): d, p = map(int, input().split()) s += d * p l.append((p,d)) l.sort() while m > 0 and l: a, b = l.pop() if m >= b: s -= a * b m -= b else: s -= a * m m = 0 print(s)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
# coding: utf-8 while 1: data=[] n,m=map(int,input().split()) if n==m==0: break for i in range(n): d,p=map(int,input().split()) data.append((d,p)) data=sorted(data,key=lambda x:(-x[1],-x[0])) for i in range(n): if data[i][0]<=m: m-=data[i][0] data[i]=(0,data[i][1]) else: data[i]=(data[i][0]-m,data[i][1]) m=0 break ans=0 for i in range(n): ans+=data[i][0]*data[i][1] print(ans)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while True: (n, m) = list(map(int, input().split())) if n == 0: break sec = [] for j in range(n): sec.append(list(map(int, input().split()))) sec.sort(key=lambda x: x[1], reverse=True) res = 0 for e in sec: if e[0] <= m: m -= e[0] elif m == 0: res += e[0] * e[1] else: res += e[1] * (e[0] - m) m = 0 print(res)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
// AOJ 2019 "お姫様の嫁入り" (ICPC模擬国内予選 2008 Problem B) import java.util.Scanner; public class Main { public static void main(String[] args) { final int MAX_P = 10; Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt(); int m = sc.nextInt(); if (n == 0) { break; } int num = 0; // 刺客に襲われる回数の期待値の最大値 int[] e = new int[MAX_P+1]; // e[i]: 期待値iの総距離長 for (int i = 0; i < n; i++) { int d = sc.nextInt(); int p = sc.nextInt(); e[p] += d; num += d * p; } for (int i = MAX_P; i >= 1; i--) { int pay = Math.min(e[i], m); num -= i * pay; m -= pay; if (m <= 0) { break; } } System.out.println(num); } } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <cstdio> #include <iostream> #include <algorithm> #define F first #define S second using namespace std; int n; long long m; int d; int p; pair<int,int> a[11111]; int main(void){ while(cin >> n >> m){ if(!n) break; for(int i = 0; i < n; i++){ cin >> d >> p; a[i] = make_pair(p,d); } sort(a,a+n,greater<pair<int,int> >()); long long res = 0; for(int i = 0; i < n; i++){ if(!m){ res += a[i].F * a[i].S; }else if(a[i].S < m){ m -= a[i].S; }else{ res += a[i].F * (a[i].S-m); m = 0; } } cout << res << endl; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <utility> #include <algorithm> #include <iostream> using namespace std; int N, M; pair<int, int> PD[10010]; int main() { while (cin >> N >> M && N) { int d, p; for (int i=0; i<N; ++i) { cin >> d >> p; PD[i] = make_pair(p, d); } sort (PD, PD+N); reverse(PD, PD+N); // sort in descending order int S = 0; for (int i=0; i<N; ++i) S += PD[i].first * PD[i].second; // When money runs out, S is the answer for (int i = 0; i<N; ++i) { if (M <= 0) break; int guarded = min(M, PD[i].second); S -= PD[i].first * guarded; M -= guarded; } cout << S << endl; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <algorithm> using namespace std; int main(){ int n,m; while(cin>>n>>m,n){ int data[11]={0}; for(int i=0;i<n;i++){ int d,p; cin>>d>>p; data[p]+=d; } int ans=0; for(int i=10;i>0;i--){ if(data[i]<=m){ m-=data[i]; data[i]=0; }else{ data[i]-=m; m=0; } ans+=(data[i]*i); } cout<<ans<<endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<bits/stdc++.h> using namespace std; int d[10000]; int main(){ while(1){ int n,m; cin >> n >> m; if(n==0 and m==0) break; pair<int,int> p[n]; for(int i=0;i<n;i++){ cin >> p[i].second >> p[i].first; } sort(p,p+n); for(int i=n-1;i>=0;i--){ if(p[i].second<m){ m-=p[i].second; p[i].second=0; }else{ p[i].second-=m; m=0; } } int sum=0; for(int i=0;i<n;i++){ sum+=(p[i].first*p[i].second); } cout << sum << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
from operator import itemgetter def protect(N,M): DP = [] for i in range(N): DP.append(list(map(int, input().split()))) DP.sort(key=itemgetter(1)) nokori = 0 for i in range(N): if M > DP[N - i - 1][0]: M -= DP[N - i - 1][0] DP.pop() elif M <= DP[N - i - 1][0]: nokori = (DP[N - i - 1][0] - M) * DP[N - i - 1][1] M = 0 DP.pop() break for i in range(len(DP)): nokori += DP[i][0] * DP[i][1] print(nokori) while True: NM = input().split() N = int(NM[0]) M = int(NM[1]) if N==0: break protect(N,M)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#! /usr/bin/python # -*- coding: utf-8 -*- (n, m) = map(int, raw_input().split()) while n!=0: Sum = 0 pd = [] for i in range(n): (d, p) = (map(int, raw_input().split())) Sum += d*p pd.append([p, d]) pd.sort(reverse=True) red = 0 for i in range(n): if m >= pd[i][1]: m -= pd[i][1] red += pd[i][0]*pd[i][1] else: red += m*pd[i][0] break print Sum-red (n, m) = map(int, raw_input().split())
PYTHON
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <algorithm> #include <iostream> #include <utility> using namespace std; int N, M; pair<int,int> PD[10010]; int main(){ while(cin >> N >> M && N){ int d, p; for (int i=0; i<N; ++i){ cin >> d >> p; PD[i] = make_pair(p,d); } sort(PD,PD+N,greater<pair<int,int>>()); int S = 0; for (int i=0; i<N; ++i){ S += PD[i].first * PD[i].second; } for (int i=0; i<N; ++i){ if (M <= 0){ break; } int guarded = min(M,PD[i].second); S -= PD[i].first * guarded; M -= guarded; } cout << S << endl; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<cstdio> #include<algorithm> #include<string> #include<cstring> #include<iostream> #include<vector> #include<map> using namespace std; typedef pair<int,int> P; #define F first #define S second int N,M; P p[10003]; int main(){ while(1){ scanf("%d %d",&N,&M); if(!N && !M) break; for(int i=0;i<N;i++){ int d,pa; scanf("%d %d",&d,&pa); p[i]=P(pa,d); } sort(p,p+N); int res=0; for(int i=N-1;i>-1;i--){ if(M==0) res+=p[i].S*p[i].F; else if(M>p[i].S) M-=p[i].S; else if(M<=p[i].S){ p[i].S-=M; M=0; res+=p[i].S*p[i].F; } } printf("%d\n",res); } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <bits/stdc++.h> using namespace std; int main(){ int n,m; while(1){ cin>>n>>m; if(!n&&!m)break; int data[11]; memset(data,0,sizeof(data)); while(n--){ int d,p; cin>>d>>p; data[p]+=d; } int k=11,ans=0; while(--k){ if(data[k]>0){ if(data[k]>=m){ data[k]-=m; for(int i=k;i>0;i--){ ans+=i*data[i]; } break; }else{ m-=data[k]; } } } cout<<ans<<endl; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main(){ int n; long long m; long long c=0; while(1){ c=0; cin >> n >> m; if(n==0&&m==0)break; pair<int,int> a[10000]; for(int i=0;i<n;i++){ cin >> a[i].second>>a[i].first; c+=a[i].first*a[i].second; } sort(a,a+n); for(int i=n-1;i>=0;i--){ if(m>a[i].second){ m-=a[i].second; c-=a[i].second*a[i].first; } else{ c-=a[i].first*m; break; } } cout << c << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashSet; import java.util.NoSuchElementException; import java.util.Objects; import java.util.PriorityQueue; import java.util.TreeSet; import java.util.function.BiFunction; public class Main{ public static void main(String[] args){ FastScanner sc = new FastScanner(); Mathplus mp = new Mathplus(); PrintWriter out = new PrintWriter(System.out); while(true){ int N = sc.nextInt(); int M = sc.nextInt(); if(N==0)return; long ans = 0; long[] L = new long[11]; for(int i=0;i<N;i++){ int d = sc.nextInt(); int p = sc.nextInt(); ans += d*p; L[p] += d; } for(int i=10;i>0;i--){ if(M>L[i]){ M -= L[i]; ans -= i*L[i]; }else{ ans -= i*M; M = 0; } } System.out.println(ans); } } } class SegmentTree<T,E>{ int N; BiFunction<T,T,T> f; BiFunction<T,E,T> g; T d1; ArrayList<T> dat; SegmentTree(BiFunction<T,T,T> F,BiFunction<T,E,T> G,T D1,T[] v){ int n = v.length; f = F; g = G; d1 = D1; init(n); build(v); } void init(int n) { N = 1; while(N<n)N*=2; dat = new ArrayList<T>(); } void build(T[] v) { for(int i=0;i<2*N;i++) { dat.add(d1); } for(int i=0;i<v.length;i++) { dat.set(N+i-1,v[i]); } for(int i=N-2;i>=0;i--) { dat.set(i,f.apply(dat.get(i*2+1),dat.get(i*2+2))); } } void update(int k,E a) { k += N-1; dat.set(k,g.apply(dat.get(k),a)); while(k>0){ k = (k-1)/2; dat.set(k,f.apply(dat.get(k*2+1),dat.get(k*2+2))); } } T query(int a,int b, int k, int l ,int r) { if(r<=a||b<=l) return d1; if(a<=l&&r<=b) return dat.get(k); T vl = query(a,b,k*2+1,l,(l+r)/2); T vr = query(a,b,k*2+2,(l+r)/2,r); return f.apply(vl, vr); } T query(int a,int b){ return query(a,b,0,0,N); } } class LazySegmentTree<T,E> extends SegmentTree<T,E>{ BiFunction<E,E,E> h; BiFunction<E,Integer,E> p = (E a,Integer b) ->{return a;}; E d0; ArrayList<E> laz; LazySegmentTree(BiFunction<T,T,T> F,BiFunction<T,E,T> G,BiFunction<E,E,E> H,T D1,E D0,T[] v){ super(F,G,D1,v); int n = v.length; h = H; d0 = D0; Init(n); } void build() { // TODO 自動生成されたメソッド・スタブ } void Init(int n){ laz = new ArrayList<E>(); for(int i=0;i<2*N;i++) { laz.add(d0); } } void eval(int len,int k) { if(laz.get(k).equals(d0)) return; if(k*2+1<N*2-1) { laz.set(k*2+1,h.apply(laz.get(k*2+1),laz.get(k))); laz.set(k*2+2,h.apply(laz.get(k*2+2),laz.get(k))); } dat.set(k,g.apply(dat.get(k), p.apply(laz.get(k), len))); laz.set(k,d0); } T update(int a,int b,E x,int k,int l,int r) { eval(r-l,k); if(r<=a||b<=l) { return dat.get(k); } if(a<=l&&r<=b) { laz.set(k,h.apply(laz.get(k),x)); return g.apply(dat.get(k),p.apply(laz.get(k),r-l)); } T vl = update(a,b,x,k*2+1,l,(l+r)/2); T vr = update(a,b,x,k*2+2,(l+r)/2,r); dat.set(k,f.apply(vl,vr)); return dat.get(k); } T update(int a,int b,E x) { return update(a,b,x,0,0,N); } T query(int a,int b,int k,int l,int r) { eval(r-l,k); if(r<=a||b<=l) return d1; if(a<=l&&r<=b) return dat.get(k); T vl = query(a,b,k*2+1,l,(l+r)/2); T vr = query(a,b,k*2+2,(l+r)/2,r); return f.apply(vl, vr); } T query(int a,int b){ return query(a,b,0,0,N); } } class UnionFindTree { int[] root; int[] rank; int[] size; UnionFindTree(int N){ root = new int[N]; rank = new int[N]; size = new int[N]; for(int i=0;i<N;i++){ root[i] = i; size[i] = 1; } } public int find(int x){ if(root[x]==x){ return x; }else{ return find(root[x]); } } public void unite(int x,int y){ x = find(x); y = find(y); if(x==y){ return; }else{ if(rank[x]<rank[y]){ root[x] = y; size[y] += size[x]; }else{ root[y] = x; size[x] += size[y]; if(rank[x]==rank[y]){ rank[x]++; } } } } public boolean same(int x,int y){ return find(x)==find(y); } } class Graph { ArrayList<Edge>[] list; int size; TreeSet<LinkEdge> Edges = new TreeSet<LinkEdge>(new LinkEdgeComparator()); @SuppressWarnings("unchecked") Graph(int N){ size = N; list = new ArrayList[N]; for(int i=0;i<N;i++){ list[i] = new ArrayList<Edge>(); } } void addEdge(int a,int b){ list[a].add(new Edge(b,1)); } void addWeightedEdge(int a,int b,int c){ list[a].add(new Edge(b,c)); } void addEgdes(int[] a,int[] b){ int size = a.length; for(int i=0;i<size;i++){ list[a[i]].add(new Edge(b[i],1)); } } void addWeighterEdges(int[] a ,int[] b ,int[] c){ int size = a.length; for(int i=0;i<size;i++){ list[a[i]].add(new Edge(b[i],c[1])); } } long[] bfs(int s){ long[] L = new long[size]; for(int i=0;i<size;i++){ L[i] = -1; } L[s] = 0; ArrayDeque<Integer> Q = new ArrayDeque<Integer>(); Q.add(s); while(!Q.isEmpty()){ int v = Q.poll(); for(Edge e:list[v]){ int w = e.to; long c = e.cost; if(L[w]==-1){ L[w] = L[v] + c; Q.add(w); } } } return L; } long[] dijkstra(int s){ long[] L = new long[size]; for(int i=0;i<size;i++){ L[i] = -1; } int[] visited = new int[size]; L[s] = 0; PriorityQueue<Pair> Q = new PriorityQueue<Pair>(new SampleComparator()); Q.add(new Pair(0,s)); while(!Q.isEmpty()){ Pair C = Q.poll(); if(visited[(int)C.b]==0){ L[(int)C.b] = C.a; visited[(int) C.b] = 1; for(Edge D:list[(int) C.b]){ Q.add(new Pair(L[(int)C.b]+D.cost,D.to)); } } } return L; } long Kruskal(){ long ans = 0; UnionFindTree UF = new UnionFindTree(size); for(LinkEdge e:Edges){ if(!UF.same(e.a,e.b)){ ans += e.L; UF.unite(e.a,e.b); } } return ans; } } class Tree extends Graph{ public Tree(int N) { super(N); } long[] tyokkei(){ long[] a = bfs(0); System.out.println(); int maxdex = -1; long max = 0; for(int i=0;i<size;i++){ if(max<a[i]){ max = a[i]; maxdex = i; } } long[] b = bfs(maxdex); System.out.println(); int maxdex2 = -1; long max2 = 0; for(int i=0;i<size;i++){ if(max2<b[i]){ max2 = b[i]; maxdex2 = i; } } long[] ans = {max2,maxdex,maxdex2}; return ans; } } class LinkEdge{ long L; int a ; int b; LinkEdge(long l,int A,int B){ L = l; a = A; b = B; } public boolean equals(Object o){ LinkEdge O = (LinkEdge) o; if(O.a==this.a&&O.b==this.b&&O.L==this.L){ return true; }else{ return false; } } public int hashCode(){ return Objects.hash(L,a,b); } } class Edge{ int to; long cost; Edge(int a,long b){ to = a; cost = b; } } class LinkEdgeComparator implements Comparator<LinkEdge>{ public int compare(LinkEdge P, LinkEdge Q) { long temp = P.L-Q.L; if(temp==0){ if(P.a>Q.a){ return 1; }else{ if(P.b>Q.b){ return 1; }else{ return -1; } } } if(temp>=0){ return 1; }else{ return -1; } } } class Pair{ long a; long b; Pair(long p,long q){ this.a = p; this.b = q; } public boolean equals(Object o){ Pair O = (Pair) o; if(O.a==this.a&&O.b==this.b){ return true; }else{ return false; } } public int hashCode(){ return Objects.hash(a,b); } } class SampleComparator implements Comparator<Pair>{ public int compare(Pair P, Pair Q) { long temp = P.a-Q.a; if(temp==0){ if(P.b>Q.b){ return 1; }else{ return -1; } } if(temp>=0){ return 1; }else{ return -1; } } } class FastScanner { private final java.io.InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;} public boolean hasNext() { skipUnprintable(); return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return (minus ? -n : n); }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return (int) (minus ? -n : n); }else{ throw new NumberFormatException(); } b = readByte(); } } } class Mathplus{ int mod = 1000000007; long[] fac = new long[1000001]; boolean isBuild = false; int mindex = -1; int maxdex = -1; void buildFac(){ fac[0] = 1; for(int i=1;i<=1000000;i++){ fac[i] = (fac[i-1] * i)%mod; } isBuild = true; } public HashSet<Integer> primetable(int m) { HashSet<Integer> pt = new HashSet<Integer>(); for(int i=2;i<=m;i++) { boolean b = true; for(int d:pt) { if(i%d==0) { b = false; break; } } if(b) { pt.add(i); } } return pt; } boolean isPrime(long n){ if(n<2)return false; for(int i=2;i*i<=n;i++){ if(n%i==0) return false; } return true; } long max(long[] a){ long max = 0; for(int i=0;i<a.length;i++){ if(max<a[i]){ max =a[i]; maxdex = i; } } return max; } int max(int[] a){ int max = 0; for(int i=0;i<a.length;i++){ if(max<a[i]){ max =a[i]; maxdex = i; } } return max; } long min(long[] a){ long min = Long.MAX_VALUE; for(int i=0;i<a.length;i++){ if(min>a[i]){ min =a[i]; mindex = i; } } return min; } int min(int[] a){ int min = Integer.MAX_VALUE; for(int i=0;i<a.length;i++){ if(min>a[i]){ min =a[i]; mindex = i; } } return min; } long sum(long[] a){ long sum = 0; for(int i=0;i<a.length;i++){ sum += a[i]; } return sum; } long sum(int[] a){ long sum = 0; for(int i=0;i<a.length;i++){ sum += a[i]; } return sum; } long gcd(long a, long b){ if(a<b){ a^=b; b^=a; a^=b; } if(a%b==0){ return b; }else{ return gcd(b,a%b); } } long lcm(long a, long b){ return a / gcd(a,b) * b; } public long perm(int a,int num) { if(!isBuild) { buildFac(); } return fac[a] * (rev(fac[a-num]))%mod; } public long comb(int a,int num){ if(!isBuild){ buildFac(); } return fac[a] * (rev(fac[num])*rev(fac[a-num])%mod)%mod; } long rev(long l) { return pow(l,mod-2); } long pow(long l, int i) { if(i==0){ return 1; }else{ if(i%2==0){ long val = pow(l,i/2); return val * val % mod; }else{ return pow(l,i-1) * l % mod; } } } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < n; i++) #define INF 100000000 #define EPS 1e-10 #define MOD 1000000007 using namespace std; typedef pair<int,int> P; int n, m; P x[10000]; void solve(){ rep(i,n) cin >> x[i].second >> x[i].first; sort(x,x+n,greater<P>()); int ans = 0; rep(i,n){ if(m >= x[i].second){ m -= x[i].second; } else{ x[i].second -= m; m = 0; ans += x[i].second*x[i].first; } } cout << ans << endl; } int main(){ while(cin >> n >> m){ if(n == 0 && m == 0) break; solve(); } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.util.Arrays; import java.util.Scanner; public class Main { MyScanner sc = new MyScanner(); Scanner sc2 = new Scanner(System.in); final int MOD = 1000000007; int[] dx = { 1, 0, 0, -1 }; int[] dy = { 0, 1, -1, 0 }; void run() { for (;;) { int N = sc.nextInt(); int M = sc.nextInt(); if ((N | M) == 0) { return; } Road[] r = new Road[N]; for (int i = 0; i < N; i++) { r[i] = new Road(sc.nextInt(), sc.nextInt()); } Arrays.sort(r); int pSum = 0; for (int i = 0; i < N; i++) { int l = r[i].len; int p = r[i].p; if (M >= l) { M -= l; } else { if (M >= 0) { l -= M; M = -1; } pSum += l * p; } } System.out.println(pSum); } } class Road implements Comparable<Road> { int len; int p; /** * @param len * @param p */ public Road(int len, int p) { super(); this.len = len; this.p = p; } @Override public int compareTo(Road arg0) { // TODO 自動生成されたメソッド・スタブ return arg0.p - this.p; } } public static void main(String[] args) { new Main().run(); } void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } void debug2(int[][] array) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]); } System.out.println(); } } boolean inner(int h, int w, int lim) { return 0 <= h && h < lim && 0 <= w && w < lim; } void swap(int[] x, int a, int b) { int tmp = x[a]; x[a] = x[b]; x[b] = tmp; } // find minimum i (a[i] >= border) int lower_bound(int a[], int border) { int l = -1; int r = a.length; while (r - l > 1) { int mid = (l + r) / 2; if (border <= a[mid]) { r = mid; } else { l = mid; } } // r = l + 1 return r; } class MyScanner { int nextInt() { try { int c = System.in.read(); while (c != '-' && (c < '0' || '9' < c)) c = System.in.read(); if (c == '-') return -nextInt(); int res = 0; do { res *= 10; res += c - '0'; c = System.in.read(); } while ('0' <= c && c <= '9'); return res; } catch (Exception e) { return -1; } } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String next() { try { StringBuilder res = new StringBuilder(""); int c = System.in.read(); while (Character.isWhitespace(c)) c = System.in.read(); do { res.append((char) c); } while (!Character.isWhitespace(c = System.in.read())); return res.toString(); } catch (Exception e) { return null; } } int[] nextIntArray(int n) { int[] in = new int[n]; for (int i = 0; i < n; i++) { in[i] = nextInt(); } return in; } int[][] nextInt2dArray(int n, int m) { int[][] in = new int[n][m]; for (int i = 0; i < n; i++) { in[i] = nextIntArray(m); } return in; } double[] nextDoubleArray(int n) { double[] in = new double[n]; for (int i = 0; i < n; i++) { in[i] = nextDouble(); } return in; } long[] nextLongArray(int n) { long[] in = new long[n]; for (int i = 0; i < n; i++) { in[i] = nextLong(); } return in; } } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<bits/stdc++.h> int n,m,i,d,p,z; int main(){ while(std::cin>>n>>m,n){ int x[11]={}; for(z=i=0;i++<n;x[p]+=d)std::cin>>d>>p; for(i=10;i>=0;z+=i*x[i],--i)x[i]-=d=(x[i]<m?x[i]:m),m-=d; std::cout<<z<<'\n'; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<iostream> #include<algorithm> #include<utility> #define loop(i,a,b) for(int i=a;i<b;i++) #define rep(i,a) loop(i,0,a) using namespace std; typedef pair<int,int> p; typedef pair<int,p> pip; int main(){ int n,m; while(cin>>n>>m,n||m){ int pin,din; pip d[n]; rep(i,n){ cin>>din>>pin; d[i]=pip(pin,p(din,i)); } sort(d,d+n); for(int i=n-1;i>=0;i--){ if(d[i].second.first<=m){ m-=d[i].second.first; d[i].second.first=0; }else{ d[i].second.first-=m; m=0; } if(m<=0)break; } int sum=0; rep(i,n){ sum+=d[i].second.first*d[i].first; } cout<<sum<<endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <bits/stdc++.h> using namespace std; int main(){ int n,m,a,b,ans; while(1){ cin>>n>>m; if(n==0) break; ans=0; vector<pair<int,int> > D; for(int i=0;i<n;i++){ cin>>b>>a; D.push_back(make_pair(a,b)); } sort(D.begin(),D.end(),greater<pair<int,int> >()); for(int i=0;i<n;i++){ if(m<0) m=0; if(m-D[i].second<0){ ans+=(D[i].second-m)*D[i].first; m=0; } else m-=D[i].second; } cout<<ans<<endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<iostream> #include<vector> #include<algorithm> using namespace std; class zone{ public: int d,p; zone(int d,int p):d(d),p(p){} }; bool operator < (zone a,zone b){ return a.p>b.p; } int main() { int n; long long m; while(cin>>n>>m && n!=0){ vector<zone> V; for(int i=0;i<n;i++){ int d,p; cin>>d>>p; V.push_back(zone(d,p)); } sort(V.begin(),V.end()); int ans=0; for(int i=0;i<V.size();i++){ if(m-V[i].d>=0) m-=V[i].d; else{ ans+=V[i].p*(V[i].d-m); m=0; } } cout<<ans<<endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(int)(b);i++) #define rep(i,n) FOR(i,0,n) #define RALL(x) (x).rbegin(),(x).rend() #define F first #define S second void solve(int n, int m) { vector<pair<int,int>> data(n); // (p,d) for(auto& i : data) cin >> i.S >> i.F; sort(RALL(data)); int ans = 0; for(auto& i : data) { if(m - i.S >= 0) { m -= i.S; } else { ans += i.F * (i.S - m); m = 0; } } cout << ans << "\n"; } int main() { int n,m; while(cin>>n>>m, n) solve(n,m); return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<algorithm> #include<iostream> using namespace std; int N,M; pair<int,int> PD[10010]; int main() { while(cin >> N >> M && N) { int d,p; for(int i=0;i<N;i++) { cin >> d >>p; PD[i]=make_pair(p,d); } sort(PD,PD+N); int S=0; for(int i=0;i<N;i++) S+=PD[i].first*PD[i].second; for(int i=N-1;i>=0;i--) { if(M<=0) break; int guard=min(M,PD[i].second); S-=PD[i].first*guard; M-=guard; } cout << S <<endl; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import static java.lang.Integer.parseInt; /** * Princess's Marriage - Accepted */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = br.readLine()) != null && !line.isEmpty()) { int N, M; N = parseInt(line.substring(0, line.indexOf(' '))); M = parseInt(line.substring(line.indexOf(' ') + 1)); if ((N | M) == 0) break; int[][] dp = new int[N][3]; int expected = 0; for (int i = 0; i < N; i++) { line = br.readLine(); dp[i][0] = parseInt(line.substring(0, line.indexOf(' '))); dp[i][1] = parseInt(line.substring(line.indexOf(' ') + 1)); dp[i][2] = dp[i][0] * dp[i][1]; expected += dp[i][2]; } Arrays.sort(dp, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return Integer.compare(o1[1], o2[1]); } }); for (int i = N - 1; i >= 0; i--) { if (dp[i][0] <= M) { expected -= dp[i][2]; M -= dp[i][0]; } else { expected -= M * dp[i][1]; M = 0; } if (M == 0) break; } System.out.println(expected); } //end while } //end main }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <cstdlib> #include <iostream> #include <vector> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); for(int n, m; cin >> n >> m, n;) { vector<int> cnt(11, 0); for(int i = 0; i < n; ++i) { int d, p; cin >> d >> p; cnt[p] += d; } int ans = 0; for(int i = 10; i > 0; --i) { if(m >= cnt[i]) { m -= cnt[i]; } else { ans += i * (cnt[i] - m); m = 0; } } cout << ans << endl; } return EXIT_SUCCESS; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
// 2016-12-06 #include <iostream> #include <algorithm> #include <cmath> #include <vector> using namespace std; struct section { int d, p; }; int main() { int n, m, d, p; vector<section> v; while (cin >> n >> m, n) { v.clear(); for (int i = 0; i < n; i++) { cin >> d >> p; section s = {d, p}; v.push_back(s); } sort(begin(v), end(v), [](const section& x, const section& y) { return x.p > y.p; }); p = 0; for (auto s : v) { d = min(m, s.d); m -= d; p += (s.d - d) * s.p; } cout << p << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <bits/stdc++.h> using namespace std; typedef pair<long long, long long> P; void solve(int n, int m){ int sum = 0; int ans = 0; vector<P> v; for(int i=0; i<n; ++i){ int d, p; cin >> d >> p; v.push_back(P(p, d)); } sort(v.begin(), v.end()); for(int i=n-1; i>=0; --i){ int d = v[i].second; int p = v[i].first; if(m - sum >= d) sum += d; else if(m == sum) ans += d * p; else{ ans += (d - m + sum) * p; m = sum; } } cout << ans << "\n"; } int main(){ // cin.tie(0); // ios::sync_with_stdio(false); while(1){ int n, m; cin >> n >> m; if(n == 0 && m == 0) return 0; solve(n, m); } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int N, M; while(cin >> N >> M && (N|M)) { vector<pair<int, int> > vec; int expect = 0; for(int i=0; i<N; i++) { int d, p; cin >> d >> p; vec.push_back(make_pair(p, d)); expect += d * p; } sort(vec.begin(), vec.end(), greater<pair<int, int> >()); for(int i=0; i<N; i++) { expect -= vec[i].first * min(vec[i].second, M); M -= vec[i].second; if(M <= 0) break; } cout << expect << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<cstdio> #define MIN(X,Y) ((X<Y)?(X):(Y)) int main(void) { while(true) { int n,m,dis[11]={}; scanf("%d%d",&n,&m); if(!n) break; for(int i=0; i<n; i++) { int d,p; scanf("%d%d",&d,&p); dis[p] += d; } for(int i=10; i; i--) { int k = MIN(dis[i], m); dis[i] -= k; m -= k; } int res=0; for(int i=0; i<=10; i++) { res += dis[i] * i; } printf("%d\n",res); } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <map> #include <algorithm> using namespace std; typedef pair<int, int> P; int n, m; P info[10001]; int main() { while (cin >> n >> m, n || m) { long long ans = 0; for (int i = 0; i < n; i++) cin >> info[i].second >> info[i].first; sort(info, info + n); for (int i = n - 1; i >= 0; i--) { if (m >= info[i].second) m -= info[i].second; else { ans += (info[i].second - m) * info[i].first; m = 0; } } cout << ans << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while True: n, m = map(int, input().split()) if n == 0:break l = [] for i in range(n): l.append(list(map(int, input().split()))) l.sort(key=lambda x:x[1]) l.reverse() for i in range(n): if m >= l[i][0]: m -= l[i][0] l[i][0] = 0 elif m != 0: l[i][0] -= m m = 0 else: break ans = 0 for s in l: ans += s[0] * s[1] print(ans)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import static java.lang.Integer.parseInt; /** * Princess's Gamble */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = br.readLine()) != null && !line.isEmpty()) { int N, M; N = parseInt(line.substring(0, line.indexOf(' '))); M = parseInt(line.substring(line.indexOf(' ') + 1)); if ((N | M) == 0) break; int[][] dp = new int[N][2]; int expected = 0; for (int i = 0; i < N; i++) { line = br.readLine(); dp[i][0] = parseInt(line.substring(0, line.indexOf(' '))); dp[i][1] = parseInt(line.substring(line.indexOf(' ') + 1)); expected += dp[i][0] * dp[i][1]; } Arrays.sort(dp, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return Integer.compare(o1[1], o2[1]); } }); for (int i = N - 1; i >= 0; i--) { if (dp[i][0] <= M) { expected -= dp[i][0] * dp[i][1]; M -= dp[i][0]; } else if (dp[i][0] > M) { expected -= M * dp[i][1]; M = 0; } if (M == 0) break; } System.out.println(expected); } //end while } //end main }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.io.InputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.InputMismatchException; import java.util.NoSuchElementException; import java.math.BigInteger; public class Main{ static PrintWriter out; static InputReader ir; static void solve(){ for(;;){ int n=ir.nextInt(); int m=ir.nextInt(); if(n==0&&m==0) return; int[][] r=new int[n][]; for(int i=0;i<n;i++) r[i]=ir.nextIntArray(2); Arrays.sort(r,new Comparator<int[]>(){ public int compare(int[] p,int[] q){ return -Integer.compare(p[1],q[1]); } }); int tot=0; for(int i=0;i<n;i++){ if(m>=r[i][0]){ m-=r[i][0]; } else{ r[i][0]-=m; m=0; tot+=r[i][0]*r[i][1]; } } out.println(tot); } } public static void main(String[] args) throws Exception{ ir=new InputReader(System.in); out=new PrintWriter(System.out); solve(); out.flush(); } static class InputReader { private InputStream in; private byte[] buffer=new byte[1024]; private int curbuf; private int lenbuf; public InputReader(InputStream in) {this.in=in; this.curbuf=this.lenbuf=0;} public boolean hasNextByte() { if(curbuf>=lenbuf){ curbuf= 0; try{ lenbuf=in.read(buffer); }catch(IOException e) { throw new InputMismatchException(); } if(lenbuf<=0) return false; } return true; } private int readByte(){if(hasNextByte()) return buffer[curbuf++]; else return -1;} private boolean isSpaceChar(int c){return !(c>=33&&c<=126);} private void skip(){while(hasNextByte()&&isSpaceChar(buffer[curbuf])) curbuf++;} public boolean hasNext(){skip(); return hasNextByte();} public String next(){ if(!hasNext()) throw new NoSuchElementException(); StringBuilder sb=new StringBuilder(); int b=readByte(); while(!isSpaceChar(b)){ sb.appendCodePoint(b); b=readByte(); } return sb.toString(); } public int nextInt() { if(!hasNext()) throw new NoSuchElementException(); int c=readByte(); while (isSpaceChar(c)) c=readByte(); boolean minus=false; if (c=='-') { minus=true; c=readByte(); } int res=0; do{ if(c<'0'||c>'9') throw new InputMismatchException(); res=res*10+c-'0'; c=readByte(); }while(!isSpaceChar(c)); return (minus)?-res:res; } public long nextLong() { if(!hasNext()) throw new NoSuchElementException(); int c=readByte(); while (isSpaceChar(c)) c=readByte(); boolean minus=false; if (c=='-') { minus=true; c=readByte(); } long res = 0; do{ if(c<'0'||c>'9') throw new InputMismatchException(); res=res*10+c-'0'; c=readByte(); }while(!isSpaceChar(c)); return (minus)?-res:res; } public double nextDouble(){return Double.parseDouble(next());} public int[] nextIntArray(int n){ int[] a=new int[n]; for(int i=0;i<n;i++) a[i]=nextInt(); return a; } public long[] nextLongArray(int n){ long[] a=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } public char[][] nextCharMap(int n,int m){ char[][] map=new char[n][m]; for(int i=0;i<n;i++) map[i]=next().toCharArray(); return map; } } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while True: N, M = map(int, input().split()) if N == 0 and M == 0: break travel = [tuple(map(int, input().split())) for _ in range(N)] travel.sort(reverse=True, key=lambda x:x[1]) for i, (d, p) in enumerate(travel): if M == 0: break travel[i] = (p, max(0, d-M)) M = max(0, M-d) print(sum(d*p for d, p in travel))
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <algorithm> using namespace std; int main(){ int n,m; while(cin>>n>>m){ if(n==0&&m==0) break; int d[n],p[n]; for(int i=0;i<n;i++){ cin>>d[i]>>p[i]; } int pm=0,nowp; while(m>0){ for(int i=0;i<n;i++){ pm=max(pm,p[i]); if(pm==p[i]) nowp=i; } if(pm==0) break; if(m>=d[nowp]){ m=m-d[nowp]; d[nowp]=0; p[nowp]=0; } else{ d[nowp]=d[nowp]-m; m=0; } pm=0; } int ptotal=0; for(int i=0;i<n;i++){ ptotal=ptotal+p[i]*d[i]; } cout<<ptotal<<endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while 1: N,M = map(int,raw_input().split()) if N == M == 0: break DP = [map(int,raw_input().split()) for _ in xrange(N)] DP = sorted(DP, key = lambda x:x[1])[::-1] ans = 0 for i in xrange(N): ans += DP[i][1]*max(0,DP[i][0]-M) M = max(0,M-DP[i][0]) print ans
PYTHON
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): rr = [] while True: n,m = LI() if n == 0 and m == 0: break a = sorted([LI()[::-1] for _ in range(n)], reverse=True) r = 0 for i in range(n): ai = a[i] if ai[1] <= m: m -= ai[1] ai[1] = 0 else: ai[1] -= m m = 0 r += ai[0] * ai[1] rr.append(r) return '\n'.join(map(str, rr)) print(main())
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <bits/stdc++.h> using namespace std; #define int long long #define Rep(i, N) for(int i = 0; i < N; i++) typedef pair<int, int> Pi; #define fi first #define se second signed main() { int N, M; Pi data[10005]; while(cin >> N >> M, N || M) { int sum = 0; Rep(i, N) { cin >> data[i].se >> data[i].fi; } sort(data, data + N, greater<Pi>()); Rep(i, N) { int val = min(M, data[i].se); data[i].se -= val; M -= val; sum += data[i].se * data[i].fi; } cout << sum << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> using namespace std; int main(){ long long int m; int n, d[10000]; short p[10000]; bool f; while( cin>>n>>m && (n||m) ){ for( int i=0;i<n;i++ ) cin >> d[i] >> p[i]; do{ f=false; for( int i=1;i<n;i++ ) if( p[i-1]<p[i] ){ int dd=d[i]; d[i]=d[i-1]; d[i-1]=dd; short pp=p[i]; p[i]=p[i-1]; p[i-1]=pp; f=true; } }while( f ); long long int ans=0; for( int i=0;i<n;i++ ){ if( m>0 ){ m-=d[i]; if( m<0 ){ d[i] = -m; m=0; ans += d[i]*p[i]; } }else if( !m ) ans += d[i]*p[i]; } cout << ans << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while True: n, m = map(int, input().split()) if (n, m) == (0, 0): break dp = [list(map(int, input().split())) for i in range(n)] dp = sorted(dp, key=lambda kv:kv[1], reverse=True) r = 0 for d,p in dp: if m >= d: m -= d else: r += p*(d-m) m = 0 print(r)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n, m; while (cin >> n >> m, n) { vector<int> cnt(11); while (n--) { int d, p; cin >> d >> p; cnt[p] += d; } for (int p = 10; p >= 0; p--) { int sub = min(m, cnt[p]); m -= sub, cnt[p] -= sub; } int ans = 0; for (int p = 0; p < 11; p++) ans += p * cnt[p]; cout << ans << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <vector> #include <algorithm> using namespace std; #define rep(i, n) for(int i=0; i<(n); ++i) #define all(c) (c).begin(), (c).end() int main(){ int N, M; while(cin >> N >> M, N|M){ vector<pair<int, int> > way; rep(i, N){ int D, P; cin >> D >> P; way.emplace_back(-P, D); } sort(all(way)); int exp = 0; rep(i, N){ M -= way[i].second; if(0 <= M)continue; exp += -way[i].first * min(-M, way[i].second); } cout << exp << '\n'; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#if 0 #endif #include <bits/stdc++.h> using namespace std; typedef long long int ll; int N, M; pair<int,int> PD[10010]; int main() { while (cin >> N >> M && N) { int d, p; for (int i=0; i<N; ++i) { cin >> d >> p; PD[i] = make_pair(p, d); } sort(PD, PD+N, greater<pair<int,int>>()); int ans = 0; for (int i = 0; i < N; i++) { int p = PD[i].first; int d = PD[i].second; if (M >= d) { M -= d; } else if (M < d && M > 0) { ans += p * (d-M); M = 0; } else { ans += p * d; } } cout << ans << endl; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.util.*; class Main { static void solve (int n, int m, ArrayList<Integer> d, ArrayList<Integer> p) { OUTER: while (m > 0 && (!p.isEmpty()) ) { int pMax = Collections.max(p); for (int i = 0; i < p.size(); i++) { if (p.get(i) == pMax) { if (m >= d.get(i)) { m -= d.get(i); p.remove(i); d.remove(i); continue OUTER; } else { d.set(i, d.get(i) - m); m = 0; continue OUTER; } } } } int res = 0; for(int i = 0; i < p.size(); i++) { res += d.get(i) * p.get(i); } System.out.println(res); } public static void main (String [] args) { Scanner sc = new Scanner(System.in); while(true) { int n = sc.nextInt(); int m = sc.nextInt(); if (n == 0 && m == 0) break; ArrayList<Integer> d = new ArrayList<Integer>(); ArrayList<Integer> p = new ArrayList<Integer>(); for(int i = 0; i < n; i++) { d.add(sc.nextInt()); p.add(sc.nextInt()); } solve(n, m, d, p); } } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.util.*; import java.io.*; import java.math.BigDecimal; import java.awt.geom.*; import static java.util.Arrays.*; import static java.lang.Math.*; public class Main{ static final Reader sc = new Reader(); static final PrintWriter out = new PrintWriter(System.out,false); public static void main(String[] args) throws Exception { while(true){ int n = sc.nextInt(); int m = sc.nextInt(); if(n==0 && m==0){ break; } int[] d = new int[10001]; for(int i=0;i<n;i++){ int x = sc.nextInt(); int y = sc.nextInt(); d[y] += x; } for(int i=10000;i>0;i--){ if(d[i]!=0){ if(d[i]>=m){ d[i] -= m; break; } else{ m -= d[i]; d[i] = 0; } } } int count = 0; for(int i=0;i<10001;i++){ count += d[i]*i; } out.println(count); out.flush(); } sc.close(); out.close(); } static void trace(Object... o) { System.out.println(Arrays.deepToString(o));} } class Reader { private final InputStream sc; private final byte[] buf = new byte[1024]; private int ptr = 0; private int buflen = 0; public Reader() { this(System.in);} public Reader(InputStream source) { this.sc = source;} private boolean hasNextByte() { if (ptr < buflen) return true; ptr = 0; try{ buflen = sc .read(buf); }catch (IOException e) {e.printStackTrace();} if (buflen <= 0) return false; return true; } private int readByte() { if (hasNextByte()) return buf[ptr++]; else return -1;} private boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} private void skip() { while(hasNextByte() && !isPrintableChar(buf[ptr])) ptr++;} public boolean hasNext() {skip(); return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); boolean minus = false; long num = readByte(); if(num == '-'){ num = 0; minus = true; }else if (num < '0' || '9' < num){ throw new NumberFormatException(); }else{ num -= '0'; } while(true){ int b = readByte(); if('0' <= b && b <= '9') num = num * 10 + (b - '0'); else if(b == -1 || !isPrintableChar(b)) return minus ? -num : num; else throw new NoSuchElementException(); } } public int nextInt() { long num = nextLong(); if (num < Integer.MIN_VALUE || Integer.MAX_VALUE < num) throw new NumberFormatException(); return (int)num; } public double nextDouble() { return Double.parseDouble(next()); } public char nextChar() { if (!hasNext()) throw new NoSuchElementException(); return (char)readByte(); } public String nextLine() { while (hasNextByte() && (buf[ptr] == '\n' || buf[ptr] == '\r')) ptr++; if (!hasNextByte()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (b != '\n' && b != '\r') { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int[] nextIntArray(int n) { int[] res = new int[n]; for (int i=0; i<n; i++) res[i] = nextInt(); return res; } public char[] nextCharArray(int n) { char[] res = new char[n]; for (int i=0; i<n; i++) res[i] = nextChar(); return res; } public void close() {try{ sc.close();}catch(IOException e){ e.printStackTrace();}}; }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <iostream> #include <string> #include <map> #include <algorithm> #include <queue> #include <cmath> using namespace std; typedef pair<int, int> P; int main() { int n; long long m; while(cin >> n >> m && (n || m)){ priority_queue<P> que; int d, p; for(int i = 0; i < n; ++i){ cin >> d >> p; que.push(P(p, d)); } long long ans = 0; while(!que.empty()){ P p = que.top(); que.pop(); m -= p.second; if(m <= 0){ ans += p.first * abs(m); break; } } while(!que.empty()){ P p = que.top(); que.pop(); ans += p.first * p.second; } cout << ans << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<iostream> #include<algorithm> using namespace std; struct I{ int d,p; }; bool operator>(const I &a,const I &b){ return a.p!=b.p?a.p>b.p:a.d>b.d; } int main(){ int n,m,ans,t,c; I i[10000]; while(cin>>n>>m,n){ ans=t=0; for(int j=0;j<n;j++)cin>>i[j].d>>i[j].p,ans+=i[j].d*i[j].p; sort(i,i+n,greater<I>()); while(m>0)c=min(i[t].d,m),m-=c,ans-=c*i[t].p,t++; cout<<max(ans,0)<<endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<iostream> #include<vector> #include<map> #include<algorithm> using namespace std; int main(){ long long int n,m; while(cin >>n>>m,n||m){ vector<pair<long long int, long long int> > road; for(int i=0,a,b; i<n; i++){ cin >>a>>b; road.push_back(make_pair(b,a)); } long long int ans = 0; sort(road.begin(),road.end()); for(int i=n-1; i>=0; i--){ if(road[i].second < m){m-=road[i].second;road[i].second = 0;} else{road[i].second-=m;m = 0;} ans+=road[i].first*road[i].second; } cout <<ans<<endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <algorithm> #include <utility> #include <iostream> using namespace std; int N, M; pair<int,int> PD[10010]; int main() { while (cin >> N >> M && N) { int d, p; for (int i=0; i<N; ++i) { cin >> d >> p; PD[i] = make_pair(p, d); } sort(PD, PD+N); reverse(PD, PD+N); int ans = 0; for (int i=0; i<N; ++i) { if (M >= PD[i].second) { M-= PD[i].second; } else { ans += PD[i].first * (PD[i].second - M); M=0; } } cout << ans << endl; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#!/usr/bin/env python # -*- coding: utf-8 -*- while True: N,M = map(int,input().split(" ")) if N==0 and M==0: break P = [list(map(int,input().split(" "))) for i in range(0,N)] ans = 0 for p in sorted(P,key=lambda x:x[1],reverse=True): ans += (p[0] - M)*p[1] if p[0] - M > 0 else 0 M = M - p[0] if M - p[0] > 0 else 0 print(ans)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; class Solve{ final Scanner in = new Scanner(System.in); boolean solve(){ int n = in.nextInt(); int m = in.nextInt(); if(n == 0) return false; Pos[] pos = new Pos[n]; for(int i=0; i<n; i++){ pos[i] = new Pos(in.nextInt(), in.nextDouble()); } Arrays.sort(pos); int res = 0; for(int i=0; i<n; i++){ if(m>0){ m -= pos[i].dist; pos[i].dist = 0; if(m < 0) pos[i].dist += -m; } res += pos[i].dist*pos[i].p; } System.out.println(res); return true; } } class Pos implements Comparable<Pos>{ int dist; double p; Pos(int d, double p){ dist = d; this.p = p; } @Override public int compareTo(Pos o) { return Double.compare(o.p, p); } } public class Main{ public static void main(String[] args) throws IOException{ Solve solve = new Solve(); while(solve.solve()); } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
# coding: utf-8 n,m=map(int,input().split()) while n!=0: d=[] p=[] for i in range(n): d_i,p_i=map(int,input().split()) d.append(d_i) p.append((p_i,i)) #タプルでインデックス情報を付加 #警備がいない場合の期待値を求める hazard=0 for i in range(n): hazard+=d[i]*p[i][0] #単位長さあたりの期待値で降順ソート p.sort(key=lambda x:-x[0]) for i in range(n): rest=d[p[i][1]] if m<rest: hazard-=m*p[i][0] break else: m-=rest hazard-=rest*p[i][0] print(hazard) n,m=map(int,input().split())
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<stdio.h> #include<algorithm> using namespace std; pair<int,int> v[10000]; int main(){ int a,b; while(scanf("%d%d",&a,&b),a){ int ret=0; for(int i=0;i<a;i++){ int c,d; scanf("%d%d",&c,&d); ret+=c*d; v[i]=make_pair(-d,c); } std::sort(v,v+a); for(int i=0;i<a;i++){ if(b>=v[i].second){ b-=v[i].second; ret+=v[i].first*v[i].second; }else{ ret+=v[i].first*b; b=0; } } printf("%d\n",ret); } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import static java.lang.Integer.parseInt; /** * Princess's Marriage - Accepted */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = br.readLine()) != null && !line.isEmpty()) { int N, M; N = parseInt(line.substring(0, line.indexOf(' '))); M = parseInt(line.substring(line.indexOf(' ') + 1)); if ((N | M) == 0) break; int[] ps = new int[11]; int expected = 0; for (int i = 0; i < N; i++) { line = br.readLine(); int D, P; D = parseInt(line.substring(0, line.indexOf(' '))); P = parseInt(line.substring(line.indexOf(' ') + 1)); ps[P] += D; expected += D * P; } for (int i = 10; i >= 0; i--) { if (ps[i] > 0) { if (ps[i] <= M) { expected -= i * ps[i]; M -= ps[i]; } else { expected -= i * M; M = 0; } } } System.out.println(expected); } //end while } //end main }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while True: n, m = map(int, input().split()) if n == 0: break risks = [(p, d) for d, p in (map(int, input().split()) for _ in range(n))] risks.sort() remain = 0 while risks: p, d = risks.pop() if d >= m: remain = p * (d - m) break m -= d print(remain + sum(p * d for p, d in risks))
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while True: n, m = map(int, input().split()) dpList = [] if (n,m) == (0,0): break for i in range(n): row = list(map(int,input().split())) dpList.append(row) dpList = sorted(dpList, key = lambda x:x[1], reverse = True) num = 0 for i in range(n): if m >= dpList[i][0]: m -= dpList[i][0] else: num += (dpList[i][0] - m) * dpList[i][1] m = 0 print(num)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <utility> #include <utility> #include <iostream> #include <algorithm> using namespace std; int main(){ int N, M; while(cin >> N >> M && N){ int d, p; pair<int, int> PD[10010]; for(int i=0; i<N; ++i){ cin >> d >> p; PD[i] = make_pair(p, d); } sort(PD, PD+N); reverse(PD, PD+N); int ex = 0; for(int i=0;i<N;++i){ if(M >= PD[i].second) M -= PD[i].second; else{ ex += (PD[i].second - M) * PD[i].first; M = 0; } } cout << ex << endl; } return 0; }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { int n = sc.nextInt();// ‹æŠÔ int m = sc.nextInt();// •P‚Ì‚¨‚©‚Ë if (n == 0 && m == 0) { break; } int c = 0; int dp[][] = new int[n][2]; for (int i = 0; i < n; i++) { dp[i][0] = sc.nextInt(); dp[i][1] = sc.nextInt(); } for (int k = 10; k > 0 && m > 0; k--) { for (int i = 0; i < n && m > 0; i++) { if (dp[i][1] == k) { if (dp[i][0] <= m) { m -= dp[i][0]; dp[i][0] = 0; } else { dp[i][0] -= m; m = 0; break; } } } } for (int i = 0; i < n; i++) { c += dp[i][0] * dp[i][1]; } System.out.println(c); } } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
while True: n, m = map(int, input().split()) if n == 0: break lst = sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x:-x[1]) ans = 0 for d, p in lst: if m >= d: m -= d else: ans += (d - m) * p m = 0 print(ans)
PYTHON3
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
import java.util.Arrays; import java.util.Scanner; public class Main { private static class Path implements Comparable<Path>{ int dist; int p; Path(int d, int p){ this.dist = d; this.p = p; } @Override public int compareTo(Path arg0) { return this.p > arg0.p ? -1 : this.p < arg0.p ? 1 : 0; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(true){ final int N = sc.nextInt(); long M = sc.nextLong(); if(N == 0 && M == 0){ break; } Path[] ps = new Path[N]; for(int i = 0; i < N; i++){ ps[i] = new Path(sc.nextInt(), sc.nextInt()); } Arrays.sort(ps); for(int i = 0; M != 0 && i < N; i++){ if(M - ps[i].dist > 0){ M -= ps[i].dist; ps[i].dist = 0; }else{ ps[i].dist -= M; M = 0; } } int sum = 0; for(int i = 0; i < N; i++){ sum += ps[i].dist * ps[i].p; } System.out.println(sum); } } }
JAVA
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include<iostream> #include<utility> #include<algorithm> using namespace std; int N, M; pair<int, int> PD[10010]; int main() { while (cin >> N >> M && N) { int d, p; for (int i = 0; i < N; ++i) { cin >> d >> p; PD[i] = make_pair(p, d); } sort(PD, PD + N); reverse(PD, PD + N); int S = 0; for (int i = 0; i < N; ++i) S += PD[i].first * PD[i].second; for (int i = 0; i < N; ++i) { if (M <= 0) break; int guarded = min(M,PD[i].second); S -= PD[i].first * guarded; M -= guarded; } cout << S << endl; } }
CPP
p01144 Princess's Marriage
Princess'Marriage Marriage of a princess English text is not available in this practice contest. A brave princess in a poor country, knowing that gambling payouts are determined by the parimutuel method, felt more familiar with gambling and was convinced of her victory in gambling. As a result, he spent more money than ever before and lost enough to lose all the taxes paid by the people. The King, who took this situation seriously, decided to marry the princess to the neighboring country. By doing this, I thought that I would like the princess to reflect on her daily activities and at the same time deepen her friendship with neighboring countries and receive financial assistance. The princess and the prince of the neighboring country liked each other, and the kings of both countries agreed on a political marriage. The princess triumphantly went to the neighboring country with a little money in her hand. On the other hand, the motive for the princess to marry is to pursue the unilateral interests of the king, and the aide of the prince of the neighboring country who thinks that it is not pleasant shoots countless thugs along the way to make the princess dead. It was. The path the princess will take has already been decided. There are a total of L post stations on the path of the princess. For convenience, the departure and arrival points are also set as post stations, and each post station is called S1, S2, ... SL. The princess shall be in S1 first, visit the post station in ascending order (S2, S3 ... in that order), and finally go to SL. At the post station, you can pay money to hire an escort, and as long as you have the money, you can contract for as long as you like to protect the princess. The cost of hiring an escort is 1 gold per distance. Note that the princess can also partially protect the section through which she passes. The distance between Si and Si + 1 is given by Di, and the expected value of the number of times a thug is attacked per distance between Si and Si + 1 is given by Pi. Find the expected number of thugs to reach your destination when the princess has a budget of M and hires an escort to minimize the expected number of thugs. Input The input consists of multiple datasets. Each dataset has the following format. > N M > D1 P1 > D2 P2 > ... > DN PN Two integers are given in the first row of each dataset, representing the number of intervals N (1 ≤ N ≤ 10,000) and the budget M (0 ≤ M ≤ 1,000,000,000) of the princess difference, respectively. The next N lines show information about the path the princess takes. Each line contains two integers, and the i-th line is the expected value of the interval Di (1 ≤ Di ≤ 10,000) and the number of attacks when moving one unit distance between them Pi (0 ≤ Pi <= 10) ). The end of the input is represented by a data set with N = 0 and M = 0. Do not output the calculation result for this data set. Output For each dataset, output the expected number of times the princess will be attacked by thugs to your destination. Sample Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output for the Sample Input Five 140 Example Input 2 8 5 6 4 5 3 1 5 10 5 10 5 10 0 0 Output 5 140
7
0
#include <bits/stdc++.h> using namespace std; int main(void) { int n, m; while (cin >> n >> m, n) { int cnt[11] = {}; int d, p; for (int i = 0; i < n; i++) { cin >> d >> p; cnt[p] += d; } for (int i = 10; i >= 0; i--) { if (m < cnt[i]) { cnt[i] -= m; m = 0; } else { m -= cnt[i]; cnt[i] = 0; } } int ans = 0; for (int i = 0; i <= 10; i++) { ans += cnt[i] * i; } cout << ans << endl; } return 0; }
CPP