-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquares_and_Rectangles.cpp
More file actions
89 lines (67 loc) · 1.38 KB
/
Squares_and_Rectangles.cpp
File metadata and controls
89 lines (67 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Question is not available but i'm providing in simple terms..
you have to maximize the area using thses sticks..
first line is testcase
second line how many pair N
next N Line Follows:
pair of size of sticks and their quantity
You can make sqaure and Rectangles using these sticks..
Then maximize the overall area possible..
testcases like :
1
4
5 6
3 2
4 3
6 1
Here output : 38
*/
#include<bits/stdc++.h>
using namespace std;
#define vi vector<int>
vector<vector<int>> dp;
bool comp(const pair<int , int> &a , const pair<int , int > &b){
return a.first > b.first;
}
void solv(){
int n;
cin >> n ;
vector<pair<int , int>> vec;
priority_queue<pair<int , int>> q;
for(int i = 0 ; i < n ; ++i){
int a , b;
cin >> a >> b ;
vec.emplace_back(a , b);
// vec.push_back(a , b)
}
sort(vec.begin(), vec.end() , comp);
// priority_queue<pair<int , int>> vec;
int ans = 0 ;
for(int i = 0 ; i < n ; ++i){
auto pr = vec[i];
int rem = pr.second % 4;
int cost = pr.second /4;
ans += cost * pr.first * 4 ;
bool k = false;
if(rem >= 2){
if(!q.empty()){
ans += (q.top().first + pr.first)*2;
q.pop();
k = true;
}
if(!k)
q.push({pr.first , rem});
}
// cout << pr.first << " " <<ans << " " << rem << " - " ;
}
cout << ans << endl;
}
int main () {
int tt;
cin >> tt;
// tt =1;
while(tt--){
solv();
}
return 0;
}