prompt
string
response
string
Problem: 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 Solution (in CPP):
#include<stdio.h> #include<map> #include<algorithm> int main() { int N,M,i,r; while(scanf("%d%d",&N,&M),N) { std::pair<int,int>a[10000]; for(r=i=0;i<N;++i)scanf("%d%d",&a[i].second,&a[i].first); std::sort(a,a+N); for(i=N-1;i>=0;--i) if(M>=a[i].second)M-=a[i].second; else r+=(a[i].second-M)*a[i].first,M=0; printf("%d\n",r); } return 0; }
Problem: 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 Solution (in CPP):
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> P; int main(){ int N, M, d, p; while(cin >> N >> M, (N | M)){ vector<P> vec; for(int i = 0 ; i < N ; i++){ cin >> d >> p; vec.push_back(P(-p,d)); } int res = 0; sort(vec.begin(),vec.end()); for(int i = 0 ; i < N ; i++){ P now = vec[i]; if(M-now.second >= 0){ M -= now.second; }else{ if(M){ now.second -= M; M = 0; } res += now.second*(-now.first); } } cout << res << endl; } return 0; }
Problem: 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 Solution (in CPP):
#include <iostream> using namespace std; int main(){ int n; int m; while( cin >> n >> m && n ){ int a[11] = {}; for( int i = 0; i < n; i++ ){ int d, p; cin >> d >> p; a[p] += d; } int result = 0; for( int i = 10; i > 0 && m; i-- ){ while( m ){ if( a[i] > 0 ){ a[i]--; m--; }else{ break; } } } for( int i = 1; i <= 10; i++ ){ result += a[i] * i; } cout << result << endl; } }
Problem: 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 Solution (in JAVA):
import java.util.*; public class Main{ int n,m; void solve(){ Scanner sc = new Scanner(System.in); while(true){ n = sc.nextInt(); m = sc.nextInt(); if(n==0 && m==0) break; PriorityQueue<int[]> q = new PriorityQueue<int[]>(n,new Comparator<int[]>(){ public int compare(int[] a, int[] b){ return b[1]-a[1]; } }); for(int i=0; i<n; i++){ int d = sc.nextInt(); int p = sc.nextInt(); q.add(new int[]{d,p}); } int ans = 0; while(q.size()>0){ int[] qq = q.poll(); int d = qq[0], p = qq[1]; while(m>0 && d>0){ m--; d--; } while(d>0){ d--; ans += p; } } System.out.println(ans); } } public static void main(String[] args){ new Main().solve(); } }
Problem: 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 Solution (in CPP):
#include <cstdio> #include <algorithm> using namespace std; typedef pair<int,int> pii; int N, M; pii PD[10010]; int main() { while (true) { scanf("%d %d", &N, &M); if (N == 0 && M == 0) break; for (int i=0;i<N;i++) { int D, P; scanf("%d %d", &D, &P); PD[i] = pii(-P, D); } sort(PD, PD+N); int idx = 0; while (M > 0 && idx < N) { if (PD[idx].second >= M) { PD[idx].second -= M; break; } M -= PD[idx++].second; } int ans = 0; for (int i=idx;i<N;i++) ans += -PD[i].first*PD[i].second; printf("%d\n", ans); } return 0; }
Problem: 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 Solution (in CPP):
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main () { int n, m; while (cin >> n >> m, (n || m)) { vector<pair<int,int> > v(n); for (auto &i : v) cin >> i.second >> i.first; sort(v.begin(), v.end()); int i = n-1; while (m && i >= 0) { if (v[i].second) m--, v[i].second--; else i--; } int res = 0; i = 0; while (i < v.size() && v[i].second) { res += v[i].second * v[i].first; i++; } cout << res << endl; } return 0; }
Problem: 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 Solution (in CPP):
#include <utility> #include <algorithm> #include <iostream> using namespace std; int N,M; pair<int,int> a[100000]; int main(){ while(cin >> N >> M){ if(N==0 && M==0){ return 0; } else{ for(int i=0; i<N; i++){ int d,p; cin >> d >> p; a[i] = make_pair(p,d); } sort(a, a+N); reverse(a, a+N); int S = 0; for(int k=0; k<N; k++){ S += a[k].first*a[k].second; } for(int l=0; l<N; l++){ if(M<=0)break; int guarded = min(M, a[l].second); S -= a[l].first*guarded; M -= guarded; } cout << S << endl; } } }
Problem: 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 Solution (in CPP):
#include<iostream> #include<algorithm> #include<string> #include<fstream> #define SIZE 9 using namespace std; int main(){ int N,M; int total; int d,p; pair<int,int>pd[10000+1]; while(cin>>N>>M,N){ total = 0; for(int i=0; i<N; i++){ cin>>d>>p; pd[i] = make_pair(p,d); total += p*d; } sort(pd,pd+N); reverse(pd,pd+N); for(int i=0; i<N; i++){ if(pd[i].second > M){ total -= M*pd[i].first; break; }else{ M -= pd[i].second; total -= pd[i].first*pd[i].second; } } cout<<total<<endl; } return 0; }
Problem: 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 Solution (in CPP):
#include<stdio.h> int main(){ int n,m,d,p,a,i; for( ; scanf("%d%d",&n,&m) , n|m ; printf("%d\n",a) ){ a=0; int f[12]={0}; for( i=n ; i-- ; f[p] += d ) scanf("%d%d",&d,&p); for( i=10 ; i >= 0 ; a += f[i]*i , i-- ) for( ; f[i] && m ; ) m--,f[i]--; } }
Problem: 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 Solution (in PYTHON3):
while True: n,m=map(int,input().split()) if n==0: break else: pdlist=[] for i in range(0,n): d,p=map(int,input().split()) pdlist.append([p,d]) pdlist.sort() i=n-1 while True: if i<0: break elif pdlist[i][1]-m<0: a=pdlist[i][1] pdlist[i][1]=0 m=(m-a) i=i-1 else: pdlist[i][1]=pdlist[i][1]-m break count=0 for i in range(0,n): count=count+pdlist[i][0]*pdlist[i][1] print(count)
Problem: 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 Solution (in PYTHON):
while(1): [n,m]=[int(x) for x in raw_input().split()] if n==0: break else: dlist=[] for i in range(n): [d,p]=[int(x) for x in raw_input().split()] dlist.append([d,p]) dlist.sort(key=lambda x:x[1]) P=0 while(m>0 and len(dlist)>0): mostdanger=dlist.pop() if m>=mostdanger[0]: m=m-mostdanger[0] else: P=(mostdanger[0]-m)*mostdanger[1] m=0 if len(dlist)==0: print P else: for sec in dlist: P=P+sec[0]*sec[1] print P
Problem: 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 Solution (in JAVA):
import java.util.*; public class Main { static Scanner s = new Scanner(System.in); static class Load implements Comparable<Load> { int D; int P; Load(int d, int p) { D = d; P = p; } public int compareTo(Load a){ int Pa = a.P; int Pb = this.P; return Pa-Pb; } } public static void main(String[] args) { while (true) { int N = s.nextInt(); int M = s.nextInt(); if(N==0 && M==0) break; Load[] load = new Load[N]; for(int i=0;i<N;i++){ int d = s.nextInt(); int p = s.nextInt(); load[i] = new Load(d,p); } Arrays.sort(load); int totalP =0; for(int i=0;i<N;i++){ if(M>=load[i].D){ M-=load[i].D; continue; } else{ totalP+=(load[i].D-M)*load[i].P; M=0; } } System.out.println(totalP); } } }
Problem: 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 Solution (in CPP):
#include <bits/stdc++.h> using namespace std; int main(void){ long long n, m; while(cin >> n >> m, n+m){ vector<pair<int,int> > v(n); for(int i= 0; i < n; ++i){ int a,b; cin >> a >> b; v.emplace_back(-b, a); } long long ans = 0; sort(v.begin(), v.end()); for(int i = 0; i < n; ++i){ long long d = v[i].second; long long p = -v[i].first; if(m>d){ m -= d; }else if(m==0){ ans += d*p; }else{ ans += p*(d-m); m = 0; } } cout << ans << endl; } return 0; }
Problem: 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 Solution (in CPP):
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; int main() { int N, M; while ( cin >> N >> M, N ) { vector<P> dat(N); for ( int i = 0; i < N; i++ ) { int d, p; cin >> d >> p; dat[i] = P(p, d); } sort(dat.begin(), dat.end(), greater<P>()); int ans = 0; for ( P i: dat ) { int p = i.first, d = i.second; if ( M < d ) ans += p*(d-M); M -= min(M, d); } cout << ans << endl; } return 0; }
Problem: 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 Solution (in CPP):
#include <iostream> #include <queue> using namespace std; typedef pair <int, int> P; int main() { int n; long long int m; while (cin >> n >> m) { if (n == 0 && m == 0) { break; } priority_queue<P> pq; for (int i = 0; i < n; i++) { P p; cin >> p.second >> p.first; pq.push(p); } while (!pq.empty()) { P pp = pq.top(); pq.pop(); if (m >= pp.second) { m -= pp.second; } else { pp.second -= m; pq.push(pp); break; } } long long int count = 0; while (!pq.empty()) { P pp = pq.top(); pq.pop(); count += pp.first*pp.second; } cout << count << endl; } return 0; }
Problem: 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 Solution (in JAVA):
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[][] map = new int[n][2]; for(int i=0;i<n;i++){ map[i][0] = sc.nextInt(); map[i][1] = sc.nextInt(); } Arrays.sort(map, new Comparator<int[]>(){ public int compare(int[] o1, int[] o2) { return o2[1] - o1[1]; } }); int sum = 0; boolean flag = false; for(int i=0;i<n;i++){ if(flag==false){ if(m<=map[i][0]){ sum += (map[i][0]-m)*map[i][1]; flag = true; }else{ m -= map[i][0]; } }else{ sum += map[i][0] * map[i][1]; } } System.out.println(sum); } } }
Problem: 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 Solution (in CPP):
#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,S=0; for(int i=0;i<N;i++){ cin>>d>>p; PD[i]=make_pair(p,d); S+=d*p; } sort(PD,PD+N); reverse(PD,PD+N); for(int i=0;i<N;i++){ if(M<=0)break; int pro; if(M>PD[i].second)pro=PD[i].second; else pro=M; S-=PD[i].first*pro; M-=pro; } cout<<S<<endl; } return 0; }
Problem: 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 Solution (in CPP):
#include <bits/stdc++.h> #define REP(i,n,N) for(int i=n;i<N;i++) #define p(S) cout<<S<<endl using namespace std; int main(){ int N,M; while(cin>>N>>M&&N){ int P[11]={0}; int d,p; long long int ans=0; REP(i,0,N){ cin>>d>>p; P[p]+=d; } for(int i=10;i>=0;i--){ if(P[i]>=M){ P[i]-=M; break; }else{ M-=P[i]; P[i]=0; } } REP(i,1,11){ ans+=i*P[i]; } p(ans); } return 0; }
Problem: 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 Solution (in CPP):
#include <cstdio> #include <algorithm> using namespace std; int main() { for(int tci = 0;; tci++) { int N,M; scanf("%d%d", &N, &M); if(!N) break; pair<int,int> *pd = new pair<int,int>[N]; int ept = 0; for(int i = 0; i < N; i++) { scanf("%d%d", &pd[i].second, &pd[i].first); ept += pd[i].second * pd[i].first; } sort(pd, pd+N); reverse(pd, pd+N); int x = M; for(int i = 0; i < N && x > 0; i++) { ept -= min(pd[i].second, x) * pd[i].first; x -= pd[i].second; } printf("%d\n", ept); } return 0; }
Problem: 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 Solution (in CPP):
#include <iostream> using namespace std; int main(){ int n,m; while(cin >> n >> m , n || m){ int data[11] = {0}; for(int i=0;i<n;i++){ int a,b; cin >> a >> b; data[b] += a; } for(int i=10;i>=0;i--){ if(data[i] <= m ){ m -= data[i]; data[i] = 0; }else{ data[i] -= m; m = 0; } } int ans = 0; for(int i=0;i<=10;i++) ans += i * data[i]; cout << ans << endl; } }
Problem: 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 Solution (in PYTHON3):
while True: n,m =map(int, input().split()) if n==0 and m==0: break temp=[] distance=0 for i in range(n): d,p= map(int,input().split()) distance = distance + d temp.append([p,d]) temp.sort() temp.reverse() if distance <= m: print(0) else: money=m while True: if money<= temp[0][1]: temp[0][1]-=money break else: money-=temp[0][1] temp.pop(0) ans=0 for i in range(0,len(temp)): ans+= temp[i][0]*temp[i][1] print(ans)
Problem: 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 Solution (in CPP):
#include<iostream> #include<algorithm> #include<utility> #include<cmath> using namespace std; int main() { int N,M; pair<int,int> PD[10010]; while(cin>>N>>M && N>0) { 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 j = 0; j < N; j++) { S += PD[j].first * PD[j].second; } for(int k = 0; k < N; k++ ) { if(M <= 0) break; int guarded = min(M, PD[k].second); S -= PD[k].first * guarded; M -= guarded; } cout<<S<<endl; } return 0; }
Problem: 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 Solution (in CPP):
#include <iostream> #include <utility> #include <algorithm> using namespace std; int N,M,d,p; pair<int,int> prin[20000]; int main(){ cin >> N >> M; while((N!=0)||(M!=0)) { for(int i=0; i<N; ++i) { cin >> d >> p; prin[i] = make_pair(-p,d); } sort(prin,prin+N); int money=prin[0].second; int k = 0; while(money <= M) { ++k; if(k>N-1) break; money += prin[k].second; } if(money<=M) cout << 0 << endl; else { int ex = -prin[k].first * (money - M); for(int l=k+1; l<N; ++l) { ex += -prin[l].first * prin[l].second; } cout << ex << endl; } cin >> N >> M; } }
Problem: 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 Solution (in CPP):
//10 #include<iostream> #include<algorithm> using namespace std; struct DP{ int d,p; bool operator<(DP a)const{ return p>a.p; } }; int main(){ for(int n,m;cin>>n>>m,n|m;){ DP dp[10000]; for(int i=0;i<n;i++){ cin>>dp[i].d>>dp[i].p; } sort(dp,dp+n); int p=0; for(int i=0;i<n;i++){ if(dp[i].d>m){ p+=(dp[i].d-m)*dp[i].p; m=0; }else{ m-=dp[i].d; } } cout<<p<<endl; } return 0; }
Problem: 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 Solution (in CPP):
#include <iostream> #include <map> using namespace std; int main() { int n,m,d,p; for(;;){ cin >> n >> m; if(n==0 && m==0) break; multimap<int,int,greater<int> > mp; while(n--){ cin >> d >> p; mp.insert(multimap<int,int>::value_type(p,d)); } multimap<int,int>::iterator it = mp.begin(); while(m > 0 && it != mp.end()){ if((*it).second <= m){ m -= (*it).second; mp.erase(it); it = mp.begin(); }else{ (*it).second -= m; m = 0; } } m = 0; while(it != mp.end()){ m += (*it).first * (*it).second; it++; } cout << m << endl; } return 0; }
Problem: 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 Solution (in JAVA):
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; public class Main{ Scanner sc=new Scanner(System.in); int INF=1<<28; double EPS=1e-12; int n, m; int[] D, P; void run(){ for(;;){ n=sc.nextInt(); m=sc.nextInt(); if((n|m)==0){ break; } D=new int[n]; P=new int[n]; for(int i=0; i<n; i++){ D[i]=sc.nextInt(); P[i]=sc.nextInt(); } solve(); } } void solve(){ class P{ int d, p; P(int d, int p){ this.d=d; this.p=p; } } P[] ps=new P[n]; for(int i=0; i<n; i++){ ps[i]=new P(D[i], P[i]); } sort(ps, new Comparator<P>(){ public int compare(P p1, P p2){ return -p1.p+p2.p; }; }); int sum=0; for(P p : ps){ if(p.d>m){ p.d-=m; m=0; }else{ m-=p.d; p.d=0; } sum+=p.d*p.p; } println(sum+""); } void debug(Object... os){ System.err.println(Arrays.deepToString(os)); } void print(String s){ System.out.print(s); } void println(String s){ System.out.println(s); } public static void main(String[] args){ new Main().run(); } }
Problem: 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 Solution (in CPP):
#include <cstdio> #include <iostream> #include <string> #include <vector> #include <algorithm> #include <cmath> using namespace std; #define reps(i, f, n) for (int i = f; i < (int)(n); ++i) #define rep(i, n) reps (i, 0, n) int main() { int n, m; while(scanf("%d%d", &n, &m), n){ int p[11] = {0}; rep(i, n){ int d, ps; scanf("%d%d", &d, &ps); p[ps] += d; } for(int i=10; i; --i){ if(p[i] > m){ p[i] -= m; m = 0; } else{ m -= p[i]; p[i] = 0; } if(m==0) break; } int ans = 0; rep(i, 11) ans += p[i] * i; printf("%d\n", ans); } return 0; }
Problem: 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 Solution (in CPP):
#include <cstdio> #include <utility> #include <cmath> #include <algorithm> #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; int main() { long long n,m; while(cin >> n >> m , !(n==0 && m==0)) { pair<long long,long long> a[100000]; for(int i=0;i<n;i++) { cin >> a[i].second >> a[i].first; } sort(a,a+n); reverse(a,a+n); long long risk = 0; for(int i=0;i<n;i++) { if(m==0){ risk += a[i].first * a[i].second; } else if(a[i].second <= m){ m-= a[i].second; } else { risk += (a[i].second - m)*a[i].first;m=0;} } cout << risk << endl; } return 0 ; }
Problem: 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 Solution (in CPP):
#include <iostream> #include <algorithm> #include <utility> using namespace std; int n,m; int p; int main() { while(cin >> n >> m && n){ pair<int,int> a[10000]; for (int i = 0; i < n; ++i) { cin >> a[i].second >> a[i].first; } sort(a,a+n); reverse(a,a+n); for (int i = 0; i < n; ++i) { if(m >= a[i].second)m -= a[i].second; else if( m == 0 ) p += a[i].second * a[i].first; else{ a[i].second -= m; m = 0; p += a[i].second * a[i].first; } } cout << p << endl; p=0; //???????????????????????? } return 0; }
Problem: 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 Solution (in CPP):
#include <bits/stdc++.h> using namespace std; int main() { for(int N, M; cin >> N >> M && (N|M);) { vector<pair<int,int> > v; for(int i = 0; i < N; ++i) { int D, P; cin >> D >> P; v.push_back(make_pair(P, D)); } sort(v.begin(), v.end(), greater<pair<int,int> >()); int res = 0; for(int i = 0; i < v.size(); ++i) { if(M < v[i].second) { res += (v[i].second - M) * v[i].first; M = 0; } else { M -= v[i].second; } } cout << res << endl; } return 0; }
Problem: 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 Solution (in PYTHON3):
while True: n,m = map(int,input().split()) if n+m == 0: break road = [list(map(int, input().split())) for _ in range(n) ] road = sorted(road, key=lambda x:-x[1]) d = 0 for a,b in road: if a <= m: m -= a else: d += (a - m) * b m = 0 print(d)
Problem: 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 Solution (in JAVA):
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][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 if (dp[i][0] > M) { expected -= M * dp[i][1]; M = 0; } if (M == 0) break; } System.out.println(expected); } //end while } //end main }
Problem: 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 Solution (in CPP):
#include <iostream> #include <algorithm> #include <map> #include <vector> using namespace std; typedef pair<int,int> P; int main(){ int n,m; while(cin>>n>>m,n||m){ 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(), greater<P>()); int s = 0; for(int i=0;i<n;++i) s+=v[i].first*v[i].second; for(int i=0;i<n;++i){ m-=v[i].second; s-=v[i].first*v[i].second; if(m<0){ s+=v[i].first*(-m); m=0; break; } } cout << s << endl; } return 0; }
Problem: 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 Solution (in CPP):
#include <iostream> using namespace std; int main(){ long long n, m; while (1){ cin >> n >> m; if (n == 0 && m == 0) break; long long dp[11] = { 0 }; long long d, p; for (int i = 0; i < n; i++){ cin >> d >> p; dp[p] += d; } long long sum = 0; for (int i = 10; i >= 0; i--){ if (i != 0 && m != 0){ int x = m - dp[i]; if (x < 0){ x *= -1; dp[i] = x; m = 0; } else{ m -= dp[i]; dp[i] = 0; } } sum += dp[i] * i; } cout << sum << endl; } return 0; }
Problem: 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 Solution (in CPP):
#include<iostream> #include<map> #include<queue> #define MAX 10000 #define INF 100000000 #include<vector> using namespace std; typedef pair<int,int> P; int main(){ int n,m,d,p; while(cin>>n>>m,n||m){ priority_queue<P> que; for(int i=0;i<n;i++){ cin>>d>>p; que.push(P(p,d)); } int ans=0; while(!que.empty()){ P p=que.top(); que.pop(); if(m-p.second<0){ ans+=(p.second-m)*p.first; break; } m-=p.second; } while(!que.empty()){ P p=que.top(); que.pop(); ans+=p.second*p.first; } cout<<ans<<endl; } return 0; }
Problem: 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 Solution (in CPP):
#include<bits/stdc++.h> using namespace std; #define fo(nn) for(int ii = 0; ii < nn; ii++) int main(){ int n, m, D, P; while(1){ int p[20] = {}, ans = 0; cin>>n>>m; if(n == 0 && m == 0) break; fo(n){ cin>>D>>P; p[P] += D; } for(int i = 10; i; i--){//cout<<ans<<" A "<<i<<" "<<p[i]<<" "<<m<<endl; if(m >= p[i]){ m -= p[i]; } else if(m == 0){ ans += p[i] * i; } else {//cout<<ans<<" "<<p[i]<<" "<<m<<" "<<i<<endl; ans += (p[i] - m) * i; m = 0; }//cout<<ans<<" "<<i<<" "<<p[i]<<" "<<m<<endl; } cout<<ans<<endl; } return 0; }
Problem: 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 Solution (in CPP):
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int N, M; while(cin >> N >> M, N){ vector< pair<int, int> > vp(N); int res = 0; for(int i=0;i<N;i++){ cin >> vp[i].second >> vp[i].first; res += vp[i].first * vp[i].second; } sort(vp.rbegin(), vp.rend()); for(int i=0;i<N;i++){ int dec = min(M, vp[i].second); res -= dec * vp[i].first; M -= dec; } cout << res << endl; } }
Problem: 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 Solution (in CPP):
#include<iostream> #include<algorithm> #include<vector> #include<map> using namespace std; int n,m; int main() { while(cin>>n>>m,n) { int cnt[11]={}; for(int i=0;i<n;i++) { int d,p;cin>>d>>p; cnt[p]+=d; } for(int i=10;i>=0&&m;i--) { if(m>cnt[i]) { m-=cnt[i];cnt[i]=0; } else { cnt[i]-=m; m=0; } } long long ans=0; for(int i=0;i<=10;i++)ans+=cnt[i]*i; cout<<ans<<endl; } }
Problem: 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 Solution (in CPP):
#include <iostream> using namespace std; int main(){ int n,m,d[10],a,b,t; while (1){ for (int i=0;i<10;i++)d[i]=0; t=0; cin >> n >> m; if (n==0&&m==0)break; for (int i=0;i<n;i++) { cin >> a >> b; d[b-1]+=a; } for (int i=9;i>=0;i--){ if (m==0) break; if (d[i]<=m){ m-=d[i]; d[i]=0; }else if (d[i]>m){ d[i]-=m; m=0; } } for (int i=0;i<10;i++){ t+=d[i]*(i+1); } cout << t << endl; } return 0; }
Problem: 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 Solution (in JAVA):
import java.io.IOException; import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String[] args) { while (true) { int N = nextInt(); if (N == 0) { break; } int M = nextInt(); ArrayList<Edge> list = new ArrayList<Edge>(); for (int i = 0; i < N; i++) { int D = nextInt(); int P = nextInt(); list.add(new Edge(D, P)); } Collections.sort(list); int deathP = 0; for (Edge e : list) { if (M >= e.D) { M -= e.D; } else { deathP += (e.D - M) * e.P; M = 0; } } System.out.println(deathP); } } static int nextInt() { int c; try { c = System.in.read(); while (c != '-' && (c < '0' || c > '9')) c = System.in.read(); if (c == '-') return -nextInt(); int res = 0; while (c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = System.in.read(); } return res; } catch (IOException e) { e.printStackTrace(); } return -1; } } class Edge implements Comparable<Edge> { int D, P; Edge(int d, int p) { this.D = d; this.P = p; } @Override public int compareTo(Edge arg0) { return arg0.P - this.P; } }
Problem: 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 Solution (in CPP):
#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef pair<int,int> P; int main(){ int n, m; while(cin >> n >> m, n || m){ 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()); int ans = 0; for(int i = v.size() - 1; i >= 0; i--){ if(m <= 0){ ans += v[i].first * v[i].second; } else if(m >= v[i].second){ m -= v[i].second; } else{ ans += v[i].first * (v[i].second - m); m = 0; } } cout << ans << endl; } }
Problem: 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 Solution (in CPP):
#include <cstring> #include <iostream> #include <queue> using namespace std; int main () { long long int m; int n; while (cin >> n >> m, n || m) { priority_queue<pair<int, int> > que; for (int i = 0; i < n; i++) { int d, p; cin >> d >> p; que.push(make_pair(p, d)); } int result = 0; while (!que.empty()) { pair<int, int> current = que.top(); que.pop(); int p = current.first; int d = current.second; if (m >= d) { m -= d; } else { int x = d - m; m = 0; result += x * p; } } cout << result << endl; } return 0; }
Problem: 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 Solution (in CPP):
#include<stdio.h> int main(){ int n,m,d,p,a,i; for( ; scanf("%d%d",&n,&m) , n|m ; printf("%d\n",a) ){ a=0; int f[12]={0}; for( i=n ; i-- ; f[p] += d ) scanf("%d%d",&d,&p); for( i=10 ; i >= 0 ; i-- ){ for( ; f[i] && m ; ){ m--, f[i]--; } a += f[i] * i; } } }
Problem: 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 Solution (in CPP):
#include <iostream> using namespace std; int main(){ int i,n,m,di,pi,o; int p[11]; for (i=0;i<11;i++) p[i] = 0; while(true){ cin >> n >> m; if (n == 0) break; for (i = 0;i<n;i++){ cin >> di >> pi; p[pi] += di; } i = 10; while(i>=0){ if (p[i] > m){ p[i] -= m; break; } else{ m -= p[i]; p[i] = 0; } i--; } o = 0; for(i=0;i<=10;i++){ o += i*p[i]; p[i] = 0; } cout << o << endl; } return 0; }
Problem: 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 Solution (in CPP):
#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 >> 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) { if (m >= pd[i].second) { m -= pd[i].second; pd[i].second = 0; } else { pd[i].second -= m; break; } } for(int i = 0 ; i < n ;i++){ ans += pd[i].first * (pd[i].second); } cout << ans << '\n'; } }
Problem: 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 Solution (in CPP):
#include "bits/stdc++.h" using namespace std; #define int long long #define endl '\n' int mod=1e9+7; int mod2=998244353; signed main(){ while(true){ int n,m; cin>>n>>m; if(!n&&!m)break; vector<pair<int,int> > a(n); for(int i=0;i<n;i++){ cin>>a[i].second>>a[i].first; } sort(a.begin(),a.end(),greater<pair<int,int> >()); int sum=0; int ans=0; for(int i=0;i<n;i++){ sum+=a[i].second; if(sum-a[i].second<=m&&sum>m){ ans+=(sum-m)*a[i].first; }else if(sum>m)ans+=a[i].first*a[i].second; } cout<<ans<<endl; } }
Problem: 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 Solution (in CPP):
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <numeric> #include <functional> #include <math.h> #include <stdio.h> using namespace std; int main(){ int n, m; while(cin>> n >> m, n || m){ vector<int> p_freq(11); for(int i = 0; i < n; i++) { int d, p; cin >> d >> p; p_freq[p] += d; } int count = 0; for(int i = 10; i > 0; i--) { if(m >= p_freq[i]) m -= p_freq[i]; else { count += (p_freq[i] - m) * i; m = 0; } } cout << count << endl; } return 0; }
Problem: 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 Solution (in CPP):
#include<iostream> #include<vector> #include<map> #include<algorithm> #define REP(i,n) for(int i=0;i<(int)(n);i++) using namespace std; typedef long long int integer; int main() { integer n, m; while (cin >> n >> m and n) { vector<pair<integer,integer> > p(n); REP(i, n) { integer D, P; cin >> D >> P; p[i].second = D, p[i].first = -P; } sort(p.begin(), p.end()); REP(i, n) { integer dec = min(p[i].second, m); p[i].second -= dec; m -= dec; } integer ans = 0; REP(i, n) { ans += -p[i].first * p[i].second; } cout << ans << endl; } }
Problem: 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 Solution (in JAVA):
import java.util.Arrays; import java.util.Scanner; public class Main { static Scanner sc = new Scanner(System.in); public static void main(String[] arg) { while (true) { int N = sc.nextInt(); if (N == 0) break; int M = sc.nextInt(); int[] v = new int[N]; for (int i = 0; i < N; ++i) { int D = sc.nextInt(); int P = sc.nextInt(); v[i] = (P << 16) + D; } Arrays.sort(v); int ans = 0; for (int i = N - 1; i >= 0; --i) { int D = v[i] & 0xFFFF; int P = v[i] >> 16; if (M >= D) { M -= D; } else { ans += (D - M) * P; M = 0; } } System.out.println(ans); } } }
Problem: 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 Solution (in CPP):
#include <iostream> #include <string> #include <vector> using namespace std; int main() { int N, M; while (cin >> N >> M, N) { int D, P, res = 0; int Ps[11] = {0}; for (int i=0; i<N; ++i) { cin >> D >> P; res += D * P; Ps[P] += D; } for (int i=10; i>=0; --i) { if (M <= Ps[i]) { res -= M * i; M = 0; } else { res -= Ps[i] * i; M -= Ps[i]; } } cout << res << endl; } return 0; }
Problem: 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 Solution (in CPP):
#include <iostream> #include <utility> #include <algorithm> #include <functional> using namespace std; typedef pair<int, int> pii; int main() { int N, M, ans; while(1){ cin >> N >> M; if(N == 0 && M == 0){ break; }else{ ans = 0; pii DP[N]; for(int i = 0; i < N; i++) cin >> DP[i].second >> DP[i].first; sort(DP, DP + N, greater<pii>()); for(int i = 0; i < N; i++){ while(DP[i].second > 0){ if(M) DP[i].second--; else break; M--; } } for(int i = 0; i < N; i++) ans += DP[i].first * DP[i].second; cout << ans << endl; } } }
Problem: 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 Solution (in CPP):
#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); } int S = 0 ; for (int i = 0; i < N; ++i) { S += PD[i].first * PD[i].second; } sort(PD,PD+N); for (int i=0; i<N; ++i) { if (M <= 0) break; int guarded = min(M , -1*PD[i].second); S -= -1*PD[i].first * guarded; M -= guarded; } cout << S << endl; } }
Problem: 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 Solution (in CPP):
#include<cstdio> #include<algorithm> #include<functional> using namespace std; typedef pair<int,int> pii; int main(){ for(int n,m;scanf("%d%d",&n,&m),n;){ int psum=0; static pii d_p[10000]; for(int i=0;i<n;i++){ scanf("%d%d",&d_p[i].second,&d_p[i].first); psum+=d_p[i].first*d_p[i].second; } sort(d_p,d_p+n,greater<pii>()); for(int i=0;i<n;i++){ if(m==0) break; int deltam=min(d_p[i].second,m); psum-=d_p[i].first*deltam; m-=deltam; } printf("%d\n",psum); } return 0; }
Problem: 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 Solution (in CPP):
#include <utility> #include <algorithm> #include <iostream> using namespace std; int N,M; pair<int,int> a[100000]; int main(){ while(cin >> N >> M){ if(N==0 && M==0){ return 0; } else{ for(int i=0; i<N; i++){ int d,p; cin >> d >> p; a[i] = make_pair(p,d); } sort(a, a+N); reverse(a, a+N); int S = 0; for(int k=0; k<N; k++){ S += a[k].first*a[k].second; } for(int l=0; l<N; l++){ if(M<=0)break; int guarded = min(M, a[l].second); S -= a[l].first*guarded; M -= guarded; } cout << S << endl; } } } #if 0 #endif
Problem: 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 Solution (in CPP):
#include <iostream> #include <vector> #include <utility> #include <algorithm> using namespace std; int main() { int N, M, D, P; while (cin >> N >> M, N || M) { vector< int > dp(11); for (int i = 0; i < N; i++) { cin >> D >> P; dp[P] += D; } int res = 0; for (int i = 10; i >= 0; i--) { if (dp[i] < M) { M -= dp[i]; } else { res += (dp[i] - M) * i; M = 0; } } cout << res << endl; } return 0; }
Problem: 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 Solution (in PYTHON3):
while True: n, m = (int(s) for s in input().split()) if not n: break sections = sorted(([int(s) for s in input().split()] for i in range(n)), key=lambda x: x[1], reverse=True) for i, (d, p) in enumerate(sections): m -= d if m <= 0: print(sum(s[0] * s[1] for s in sections[i+1:]) - p * m) break else: print(0)
Problem: 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 Solution (in CPP):
#include<iostream> #include<algorithm> #define f first #define s second using namespace std; int main(){ int n,m; pair<int,int>d[10000]; while(cin>>n>>m&&n!=0){ int an=0,k=0; for(int i=0;i<n;i++)cin>>d[i].s>>d[i].f,k+=d[i].s; sort(d,d+n); for(int i=0;m<k;i++){ if(k-m<d[i].s)an+=d[i].f*(k-m); else an+=d[i].f*d[i].s; k-=d[i].s; } cout<<an<<endl; } return 0; }
Problem: 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 Solution (in PYTHON3):
while 1: n, m = map(int, input().split()) if n == 0: break data = [] for _ in range(n): d, p = map(int, input().split()) data.append([d, p]) data = sorted(data, key=lambda x: -x[1]) for d in data: if m <= 0: break if d[0] <= m: m -= d[0] d[0] = 0 else: d[0] -= m m = 0 ans = 0 for d in data: ans += d[0] * d[1] print(ans)
Problem: 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 Solution (in JAVA):
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); while(true) { int n = sc.nextInt(); if(n == 0) return; long m = sc.nextLong(); int[][] d = new int[n][2]; for(int i=0; i<n; i++) { d[i][0] = sc.nextInt(); d[i][1] = sc.nextInt(); } Arrays.sort(d ,(a,b)-> Integer.compare(b[1],a[1])); int ans = 0; for(int i=0; i<n; i++) { if(d[i][0] <= m) { m -= d[i][0]; }else { ans += (d[i][0] - m) * d[i][1]; m = 0; } } System.out.println(ans); } } }
Problem: 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 Solution (in JAVA):
import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { new Main().run(); } private void run() throws IOException { Scanner scanner = new Scanner(System.in); while (true) { int n = scanner.nextInt(); int m = scanner.nextInt(); if((n|m)==0) break; int[] cnt = new int[11]; while (n-- > 0) { int d = scanner.nextInt(); int p = scanner.nextInt(); 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; } System.out.println(ans); } } }
Problem: 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 Solution (in CPP):
#include <iostream> #include <algorithm> #include <utility> #define llint long long using namespace std; typedef pair<llint, llint> P; llint n, m; P p[10005]; int main(void) { while(1){ cin >> n >> m; if(n == 0 && m == 0) break; llint x, y, ans = 0; for(int i = 1; i <= n; i++){ cin >> x >> y; p[i] = P(y, x); ans += x * y; } sort(p+1, p+n+1); reverse(p+1, p+n+1); llint rem = m; for(int i = 1; i <= n; i++){ x = min(rem, p[i].second); ans -= p[i].first * x; rem -= x; } cout << ans << endl; } return 0; }
Problem: 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 Solution (in PYTHON):
while True: N,M=map(int,raw_input().split()) if N==M==0:break L=[map(int,raw_input().split()) for i in range(N)] L=[(j,i) for i,j in L] L.sort() L=reversed(L) cost=0 for p,d in L: cost+=p*(max(d-M,0)) M=max(M-d,0) print cost
Problem: 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 Solution (in CPP):
#include<iostream> #include<map> #include<algorithm> using namespace std; pair<int,int> road[10000]; int main() { int N,M,d,p; while(cin>>N>>M, N|M) { for(int i=0; i<N; ++i) { cin>>d>>p; road[i] = pair<int,int>(p,d); } sort(road,road+N); int ans = 0; for(int i=N-1; i>=0; --i) { if(M <= 0) { ans += road[i].first*road[i].second; }else if(M <= road[i].second) { ans += road[i].first*(road[i].second-M); M = 0; } else { M -= road[i].second; } } cout<<ans<<endl; } }
Problem: 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 Solution (in JAVA):
import java.util.Arrays; import java.util.Scanner; //Princess's Marriage public class Main{ class P implements Comparable<P>{ int d, p; public P(int d, int p) { this.d = d; this.p = p; } public int compareTo(P o) { return o.p-p; } } int n, m; P[] p; void run(){ Scanner sc = new Scanner(System.in); while(true){ n = sc.nextInt(); m = sc.nextInt(); if((n|m)==0)break; p = new P[n]; int s = 0; for(int i=0;i<n;i++){ p[i]=new P(sc.nextInt(), sc.nextInt()); s += p[i].d*p[i].p; } Arrays.sort(p); int r = 0; int i = 0; while(m>0 && i<n){ int min = Math.min(m, p[i].d); r += min*p[i].p; i++; m-=min; } System.out.println(s-r); } } public static void main(String[] args) { new Main().run(); } }
Problem: 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 Solution (in CPP):
#include <bits/stdc++.h> using namespace std; int main(){ int n,m; while(cin>>n>>m,n){ vector<int>v(11,0); for(int i=0;i<n;++i){ int d,p; cin>>d>>p; v[p]+=d; } int ans=0; for(int i=10;i>0;--i){ if(m>v[i]){ m-=v[i]; v[i]=0; }else{ v[i]-=m; m=0; } ans+=v[i]*i; } cout<<ans<<endl; } }
Problem: 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 Solution (in JAVA):
import java.io.*; import java.util.*; public class Main { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); class Road implements Comparable<Road> { int d, p; Road(int d, int p) { this.d = d; this.p = p; } public int compareTo(Road s) { return s.p - p; } } public void run() { while (true) { int n = in.nextInt(), m = in.nextInt(); if (n == 0) break; Road[] road = new Road[n]; for (int i = 0; i < n; i++) { int d = in.nextInt(); int p = in.nextInt(); road[i] = new Road(d, p); } Arrays.sort(road); int res = 0; for (int i = 0; i < n; i++) { if (m >= road[i].d) { m -= road[i].d; } else { res += road[i].p * (road[i].d - m); m = 0; } } System.out.println(res); } out.close(); } public static void main(String[] args) { new Main().run(); } public void mapDebug(int[][] a) { System.out.println("--------map display---------"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.printf("%3d ", a[i][j]); } System.out.println(); } System.out.println("----------------------------"); System.out.println(); } public void debug(Object... obj) { System.out.println(Arrays.deepToString(obj)); } class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; //stream = new FileInputStream(new File("dec.in")); } int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } long nextLong() { return Long.parseLong(next()); } long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } double nextDouble() { return Double.parseDouble(next()); } double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } }
Problem: 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 Solution (in CPP):
#include <iostream> #include <string> #include <vector> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M; while (cin >> N >> M, N) { int D, P, res = 0, Ps[11] = {}; for (int i=0; i<N; ++i) { cin >> D >> P; res += D * P; Ps[P] += D; } for (int i=10; i>0; --i) { if (M <= Ps[i]) { res -= M * i; M = 0; } else { res -= Ps[i] * i; M -= Ps[i]; } } cout << res << endl; } return 0; }
Problem: 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 Solution (in PYTHON3):
while 1: N,M=map(int,input().split()) if not N and not M:break s=0 t=[] for _ in range(N): a,b=map(int,input().split()) s+=a*b t.extend([(b,a)]) for i in sorted(t)[::-1]: if i[1]<=M: s-=i[0]*i[1] M-=i[1] elif 0<M<i[1]: s-=M*i[0] M=0 break print(s)
Problem: 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 Solution (in JAVA):
import java.util.*; import static java.lang.System.*; class Main { public static Scanner sc = new Scanner(in); public static Random rand=new Random(); class D{ int d; int x; D(int d,int x){ this.d=d; this.x=x; } } public void run() { while(true){ int n=sc.nextInt(),m=sc.nextInt(); if(n==0 && m==0)return; D[] dlist=new D[n]; for(int i=0;i<n;i++){ dlist[i]=new D(sc.nextInt(),sc.nextInt()); } Arrays.sort(dlist,new Comparator<D>(){ @Override public int compare(D o1, D o2) { return o2.x-o1.x; } }); for(int i=0;i<n;i++){ if(m>=dlist[i].d){ m-=dlist[i].d; dlist[i].d=0; }else{ dlist[i].d-=m; m=0; } } int sum=0; for(int i=0;i<n;i++){ sum+=dlist[i].d*dlist[i].x; } ln(sum); } } public static void main(String[] args) { new Main().run(); } public int[] nextIntArray(int n){ int[] res=new int[n]; for(int i=0;i<n;i++){ res[i]=sc.nextInt(); } return res; } public static void pr(Object o) { out.print(o); } public static void ln(Object o) { out.println(o); } public static void ln() { out.println(); } }
Problem: 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 Solution (in CPP):
#include <algorithm> #include <iostream> #include <utility> #include <vector> using namespace std; int main() { int N, M, ans, d, p; while (cin >> N >> M, N) { vector< pair<int, int> > V; ans = 0; for (int i = 0; i < N; ++i) { cin >> d >> p; ans += d*p; V.push_back(make_pair(-p, d)); } sort(V.begin(), V.end()); for (int i = 0; i < N; ++i) { p = V[i].first; d = V[i].second; M -= d; ans += p*d; if (M <= 0) { ans += M*p; break; } } cout << ans << endl; } return 0; }
Problem: 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 Solution (in CPP):
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ int n, m; while(cin >> n >> m, n+m){ vector<pair<int,int>> v; int d, p, sum = 0; for(int i = 0; i < n; i++){ cin >> d >> p; sum += p*d; v.push_back({p,d}); } sort(v.rbegin(), v.rend()); for(int i = 0; m > 0 && i < n; i++){ int use = min(m, v[i].second); m -= use; sum -= use*v[i].first; } cout << sum << endl; } return 0; }
Problem: 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 Solution (in CPP):
#include <cstdio> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) int main(){ while(true){ int n,m; scanf("%d%d",&n,&m); if(n==0) break; int o[11]={}; rep(i,n){ int d,p; scanf("%d%d",&d,&p); o[p]+=d; } int ans=0; for(int i=10;i>=0;i--){ if(m>o[i]){ m-=o[i]; continue; }else{ ans+=i*(o[i]-m); for(int j=i-1;j>=0;j--) ans+=j*o[j]; break; } } printf("%d\n",ans); } return 0; }
Problem: 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 Solution (in CPP):
#include <bits/stdc++.h> #define REP(i, n) for(int i = 0; i < (int) (n); i++) #define ALL(v) (v).begin(), (v).end() #define INF 1e9 using namespace std; int dist[15]; int main(){ int n, m; while(cin >> n >> m and n + m){ REP(i, n){ int d, p; cin >> d >> p; dist[p] += d; } REP(i, 15){ if(dist[14-i] <= m) { m -= dist[14-i]; dist[14-i] = 0; } else if(dist[14-i] > 0) { dist[14-i] -= m; m = 0; } if(m <= 0) break; } int res = 0; REP(i, 15) res += i * dist[i]; cout << res << endl; memset(dist, 0, sizeof(dist)); } return 0; }
Problem: 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 Solution (in CPP):
#include<iostream> #include<utility> #include<algorithm> using namespace std; int main(){ int n,m; int d,p; pair<int,int> v[10010]; while(cin >> n >> m, n||m){ for(int i=0;i<n;i++){ cin >> d >> p; v[i] = make_pair(p,d); } sort(v,v+n); int cnt = n-1; while(0<=cnt && m){ if(v[cnt].second>m){ v[cnt].second -= m; m = 0; }else{ m -= v[cnt].second; v[cnt].second = 0; } cnt--; } int ans = 0; for(int i=0;i<n;i++)ans += v[i].first * v[i].second; cout << ans << endl; } }
Problem: 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 Solution (in PYTHON3):
import sys from operator import itemgetter inf = 1<<30 def solve(): while 1: N, M = map(int, sys.stdin.readline().split()) if N == M == 0: return sect = [tuple(list(map(int, sys.stdin.readline().split()))) for i in range(N)] sect.sort(key=itemgetter(1), reverse=True) ans = 0 for di, pi in sect: if di <= M: M -= di else: ans += (di - M) * pi M = 0 print(ans) if __name__ == '__main__': solve()
Problem: 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 Solution (in CPP):
#include "bits/stdc++.h" using namespace std; #define rep(i, j) for(int i = 0; i < (j); i++) int main() { while(true) { int N, M; cin >> N >> M; if(N == 0) break; vector<pair<int, int>> PD(N); rep(i, N) cin >> PD[i].second >> PD[i].first; sort(begin(PD), end(PD)); reverse(begin(PD), end(PD)); long long ans = 0; rep(i, N) { int p = PD[i].first, d = PD[i].second; int m = min(M, d); d -= m; M -= m; ans += d * p; } cout << ans << endl; } }
Problem: 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 Solution (in CPP):
#include <iostream> #include <string> #include <algorithm> #include <vector> #include <utility> #include <cstring> using namespace std; #define rep(i,n) for(int i=0; i<n; i++) typedef long long ll; int main() { int n; ll m; while(cin >> n >> m, n||m) { ll dist[11] = {}; rep(i, n) { int d, p; cin >> d >> p; dist[p] += d; } for(int i=10; i>0 && m>0; i--) { ll l = min( dist[i], m ); dist[i] -= l; m -= l; } ll res = 0; for(int i=1; i<=10; i++) { res += i * dist[i]; } cout << res << endl; } return 0; }
Problem: 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 Solution (in CPP):
#include <iostream> #include <vector> #include <string> #include <map> #include <algorithm> #define REP(i,n) for(int i=0;i<(int)(n);i++) using namespace std; int main() { while(1){ int n,m; cin>>n>>m; if(!n)break; vector<tuple<int,int>> v; REP(i,n){ int d,p; cin>>d>>p; v.emplace_back(-p,d); } sort(begin(v),end(v)); int sum=0; REP(i,n){ int d,p; tie(p,d)=v[i]; if(d <= m){ m-=d; }else{ sum += -p * (d-m); m=0; } } cout<<sum<<endl; } return 0; }
Problem: 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 Solution (in CPP):
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { int n,m; for(;cin>>n>>m,n;) { vector<pair<int,int> > set; for(int i=0;i<n;i++) { int d,p; cin>>d>>p; set.push_back(pair<int,int>(p,d)); } sort(set.begin(),set.end()); int sum=0; for(int i=n-1;i>=0;i--) { int d=set[i].second; int p=set[i].first; sum+=(max(0,d-max(m,0)))*p; m-=d; } cout<<sum<<endl; } }
Problem: 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 Solution (in JAVA):
import java.util.Scanner; public class Main { public static void main(String[] args) { try(Scanner sc = new Scanner(System.in)){ while(sc.hasNext()) { int N=sc.nextInt(); int M=sc.nextInt(); if(N+M==0) break; int[] ds = new int[11]; for(int i=0; i<N; i++) { int d = sc.nextInt(); int p = sc.nextInt(); ds[p] += d; } int P = 0; for(int p= 10; p >=0; p--) { if(M >= ds[p]) { M -= ds[p]; } else { P += (ds[p] - M) * p; M = 0; } } System.out.println(P); } } } }
Problem: 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 Solution (in CPP):
#include<iostream> #include<vector> #include<algorithm> using namespace std; struct dis{ int d, p; }; bool s(dis const& c,dis const&b){ return c.p>b.p; } int n,m,a; vector<dis>vec; int main(){ while(cin>>n>>m,n){ vec.clear(); a=0; for(int i=0;i<n;i++){ dis d; cin>>d.d>>d.p; vec.push_back(d); } sort(vec.begin(),vec.end(),s); for(int i=0;i<n;i++){ if(m<vec[i].d){ a+=(vec[i].d-m)*vec[i].p; } m=max(0,m-vec[i].d); } cout<<a<<endl; } return 0; }
Problem: 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 Solution (in PYTHON3):
while True: N, M = map(int, input().split()) if N == 0 and M == 0: break travel = [list(map(int, input().split())) for _ in range(N)] travel.sort(reverse=True, key=lambda x:x[1]) for i, (d, _) in enumerate(travel): if M == 0: break travel[i][0] = max(0, d-M) M = max(0, M-d) print(sum(d*p for d, p in travel))
Problem: 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 Solution (in CPP):
#include<iostream> #include<algorithm> using namespace std; int N, M, temp; int i=0,ans=0; pair<int,int> PD[10010]; int main() { while (cin >> N >> M && N) { int d, p; for (int j=0; j<N; ++j) { cin >> d >> p; PD[j] = make_pair(p, d); } sort(PD,PD+N); reverse(PD,PD+N); while(i<N){ temp=M; M-=PD[i].second; if(M<0){ PD[i].second-=temp; break; } else{ PD[i].second=0; i++; } } for(int k=0;k<N;k++){ ans+=PD[k].first*PD[k].second; } cout << ans << endl; ans=0; i=0; } }
Problem: 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 Solution (in CPP):
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n,m; ll ds[11]; int main(){ while(true){ scanf("%d%d", &n, &m); if(n+m==0)return 0; for(int i=0;i<=10;i++)ds[i]=0; while(n--){ ll d; int x; scanf("%lld%d",&d,&x); ds[x] += d; } ll ans = 0; for(int i=10;i>=0;i--){ ll use = min((ll)m, ds[i]); ds[i] -= use; m -= use; ans += i * ds[i]; } printf("%lld\n", ans); } }
Problem: 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 Solution (in CPP):
#include<bits/stdc++.h> using namespace std; int d[11], n, m, i, a, b, sum, e; int main() { while (true) { memset(d, 0, sizeof(d)); sum = 0; cin >> n >> m; if (!n) { break; } for (i = 0; i < n; i++) { cin >> a >> b; d[b] += a; } for (i = 10; i >= 0;i--){ e = min(d[i], m); m -= e; sum += i*(d[i] - e); } cout << sum << endl; } }
Problem: 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 Solution (in CPP):
#include<iostream> #include<algorithm> using namespace std; int main(){ while(1){ int n,m,b,d[1000000]={},a,mx=0; cin>>n>>m; if(n+m==0) break; for(int i=0;i<n;i++){ cin>>a>>b; d[b]+=a; mx=max(mx,b); } for(int i=mx;i>=0;i--){ if(d[i]>0 && m>0){ int t= d[i]; d[i]-=m; m-=t; } } int ans=0; for(int i=0;i<=mx;i++) if(d[i]>0) ans+=(i*d[i]); cout<<ans<<endl; } return 0; }
Problem: 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 Solution (in PYTHON3):
while True: n,m =map(int, input().split()) if n==0 and m==0: break a=[] length=0 for i in range(0,n): d,p= map(int,input().split()) length+= d a.append([p,d]) a.sort() a.reverse() if length <= m: print(0) else: zankin=m flag=0 while True: if zankin<= a[0][1]: a[0][1]-=zankin break else: zankin-=a[0][1] a.pop(0) ans=0 for i in range(0,len(a)): ans+= a[i][0]*a[i][1] print(ans)
Problem: 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 Solution (in CPP):
#include <iostream> #define MAX_P 10 #define rep(i,n) for(int i=0;i<(int)(n);i++) using namespace std; int main(void) { int N; long M; while(cin >> N >> M, N > 0){ long D[MAX_P + 1]; long result = 0; //配列Dを初期化 rep(i, MAX_P + 1){ D[i] = 0; } //配列Dを構成 rep(i, N){ int d, p; cin >> d >> p; D[p] += d; } rep(i, MAX_P + 1){ if (M >= D[MAX_P - i]){ M -= D[MAX_P - i]; }else if(M > 0){ result += (D[MAX_P - i]-M) * (MAX_P - i); M = 0; }else{ result += D[MAX_P - i] * (MAX_P - i); } } cout << result << endl; } return 0; }
Problem: 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 Solution (in CPP):
#include <bits/stdc++.h> using namespace std; const int MAX_N=1e4+10; int N,M,D,P,ans,d; vector<pair<int,int>> road(MAX_N); int main(){ cin.tie(0); ios::sync_with_stdio(false); while(cin >> N >> M,N){ ans=0; for (int i=0;i<N;++i){ cin >> D >> P; road[i]={-P,D}; ans+=D*P; } sort(road.begin(),road.begin()+N); for (int i=0;i<N&&M;++i){ d=min(M,road[i].second); ans+=road[i].first*d; M-=d; } cout << ans << '\n'; } }
Problem: 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 Solution (in CPP):
#include <bits/stdc++.h> using namespace std; int main() { int N, M; while(cin >> N >> M, N || M){ int D[10001] = {0}, P[10001] = {0}; long long sum = 0; for(int i = 0; i < N; i++) cin >> D[i] >> P[i], sum += D[i]*P[i]; while(M > 0){ int idx = max_element(P, P + N) - P; if(P[idx] == -1) break; sum -= min(M, D[idx])*P[idx]; M -= D[idx]; P[idx] = -1; } cout << sum << endl; } return 0; }
Problem: 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 Solution (in JAVA):
import java.util.*; class Main { public static void main(String args[]){ new Main().run(); } Scanner sc = new Scanner(System.in); void run(){ while(true){ int n = sc.nextInt(); int m = sc.nextInt(); if((n|m) == 0)return; final int ds[]=new int[n]; final int ps[]=new int[n]; Integer ns[]=new Integer[n]; for(int i = 0 ;i < n;i++){ ds[i] = sc.nextInt(); ps[i] = sc.nextInt(); ns[i] = i; } Arrays.sort(ns, new Comparator<Integer>(){ public int compare(Integer i, Integer j){ return ps[j] - ps[i]; } }); // System.err.println(Arrays.toString(ns)); int ans = 0; for(int i = 0 ; i < n ; i++){ ans += ps[ns[i]] * (ds[ns[i]] - Math.min(ds[ns[i]], m)); m -= Math.min(ds[ns[i]], m); } System.out.println(ans); } } }
Problem: 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 Solution (in CPP):
#include <cstdio> #include <vector> #include <algorithm> using namespace std; typedef long long int64; int main() { while(true) { int n, m; int64 ans = 0; vector<pair<int, int> > dp; scanf("%d%d", &n, &m); if(n == 0) break; dp.resize(n); for(int i = 0; i < n; ++i) scanf("%d%d", &dp[i].second, &dp[i].first); sort(dp.rbegin(), dp.rend()); for(int pos = 0; pos < n; ++pos) { int d = dp[pos].second; int p = dp[pos].first; int x = min(d, m); m -= x; ans += (d - x) * (int64)p; } printf("%lld\n", ans); } return 0; }
Problem: 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 Solution (in CPP):
#include <bits/stdc++.h> using namespace std; const int Ex = 11; vector<int> v; int main(){ while(1){//start int n, m; cin >> n >> m; if(n == 0 and m == 0) break; v.resize(Ex); //input data for(int i = 0; i < n; i++){ int d, p; cin >> d >> p; v[p] += d; } //sub for(int i = 10; i > 0; i--){ if(m - v[i] >= 0){ m -= v[i]; v[i] = 0; } else{ v[i] -= m; break; } } //debug //for(int i = 1; i < 11; i++) cout << "test = " << v[i] << endl; //cluc int ans = 0; for(int i = 10; i > 0; i--){ ans += i * v[i]; } cout << ans << endl; v.clear(); }//end return 0; }
Problem: 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 Solution (in CPP):
#include<map> #include<algorithm> #include<iostream> #include<string> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> using namespace std; int main(){ int i; int b,m,n; map<int,int,greater<int> > a; map<int,int,greater<int> >::iterator it; while(cin>>n>>m&&n+m){ a.clear(); for(i=0;i<n;i++){ int d,p; cin>>d>>p; a[p]+=d; } b=0; for(it=a.begin();it!=a.end();it++){ if((*it).second>m){ b+=(*it).first*((*it).second-m); m=0; }else m-=(*it).second; } cout<<b<<endl; } return 0; }
Problem: 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 Solution (in CPP):
#include<bits/stdc++.h> #define _ ios_base::sync_with_stdio(0);cin.tie(0); #define REP(i,n) for(int i=0;i<(int)(n);i++) using namespace std; const int MAX_N=10000; pair<int,int> dp [MAX_N]; int n,m; void solve(){ sort(dp,dp+n,greater<pair<int,int> >()); for(int i=0;i<n;i++){ int tmp=dp[i].second; if(tmp>=m){ dp[i].second-=m; break; }else{ dp[i].second=0; m-=tmp; } } long long int sum=0; REP(i,n){ sum+=dp[i].first*dp[i].second; } cout<<sum<<endl; } int main(){ _; while(cin>>n>>m,(n|m)!=0){ REP(i,n)cin>>dp[i].second>>dp[i].first; solve(); } }
Problem: 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 Solution (in CPP):
#include<iostream> #include<algorithm> #include<utility> using namespace std; long long N, M,sum,t; pair <long long, long long> p[30000]; int main(){ while ((cin >> N >> M), N){ sum = 0; for (int i = 0; i < N; i++){ cin >> p[i].second >> p[i].first; sum += p[i].second*p[i].first; } sort(p, p + N); reverse(p, p + N); for (int i = 0; i < N; i++){ if (M <= 0)break; int guarded = min(p[i].second, M); sum -= p[i].first*guarded; M -= guarded; } cout << sum << endl; } }
Problem: 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 Solution (in CPP):
#include <iostream> #include <algorithm> #include <cassert> #include <cctype> #include <complex> #include <cstdio> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) int N,M; pair<int,int> p[10000]; int main(){ while(cin>>N>>M&&N){ int ans=0; rep(i,N){ cin>>p[i].second>>p[i].first; ans+=p[i].second*p[i].first; p[i].first*=-1; } sort(p,p+N); rep(i,N){ if(M>=p[i].second){M-=p[i].second;ans+=p[i].second*p[i].first;} else{ans+=M*p[i].first;break;} } cout<<ans<<endl; } }
Problem: 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 Solution (in CPP):
#include <utility> #include <algorithm> #include <iostream> using namespace std; int main() { int N, M; while(cin >> N >> M && N >0) { pair<int, int> PD[10000]; for(int i=0;i<N;i++) cin >> PD[i].second >> PD[i].first; sort(PD, PD+N); int Mcur = M; int sum = 0; for(int i=0;i<N;i++) sum += PD[i].first * PD[i].second; for(int i=N-1;i>=0;i--) { if(Mcur <= PD[i].second) { sum -= Mcur*PD[i].first; break; } else { sum -= PD[i].first*PD[i].second; Mcur -= PD[i].second; } } cout << sum << endl; } }
Problem: 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 Solution (in CPP):
#include<iostream> #include<algorithm> #include<vector> #include<map> using namespace std; typedef pair< int , int > P; int main(){ int N, M; while(cin >> N >> M , N){ int d[11] = {}; for(int i = 0 ; i < N ; i++ ){ int a, b; cin >> a >> b; d[b] += a; } for(int i = 10 ; i && M ; i-- ){ int t = min( d[i], M); d[i] -= t; M -= t; } int ret = 0; for(int i = 0 ; i < 11 ; i++ ) ret += d[i] * i; cout << ret << endl; } }
Problem: 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 Solution (in PYTHON3):
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)