diff --git a/2020/Div 3/661 Div 3/Programs/Gifts Fixing.cpp b/2020/Div 3/661 Div 3/Programs/Gifts Fixing.cpp index fa6a0be5..6a23db45 100644 --- a/2020/Div 3/661 Div 3/Programs/Gifts Fixing.cpp +++ b/2020/Div 3/661 Div 3/Programs/Gifts Fixing.cpp @@ -1,54 +1,54 @@ -#include -#include -#include - -#define all(v) (v).begin(), (v).end() +#include +#include +#define lli long long int +#define pb push_back +#define cio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) +#define rep(i,a,n) for(i=a;i> no_of_elements; - - vector A(no_of_elements + 1); - for(int i = 1; i <= no_of_elements; i++) - { - cin >> A[i]; - } - - vector B(no_of_elements + 1); - for(int i = 1; i <= no_of_elements; i++) - { - cin >> B[i]; - } - - const int oo = 1e9 + 9; - int target_A = oo, target_B = oo; - - for(int i = 1; i <= no_of_elements; i++) - { - target_A = min(target_A, A[i]); - target_B = min(target_B, B[i]); - } - - long long no_of_moves = 0; - for(int i = 1; i <= no_of_elements; i++) - { - long long a_moves_here = A[i] - target_A, b_moves_here = B[i] - target_B; - - no_of_moves += max(a_moves_here, b_moves_here); - } - - cout << no_of_moves << "\n"; -} - int main() { - int no_of_test_cases; - cin >> no_of_test_cases; - - while(no_of_test_cases--) - solve(); - - return 0; + cio; + lli t,n,i,ma,mb,k,ans=0; + cin>>t; + while(t--){ + cin>>n; + ma=1e18; + mb=1e18; + ans=0; + lli a[n],b[n]; + for(i=0;i>a[i]; + ma=min(ma,a[i]); + } + for(i=0;i>b[i]; + mb=min(mb,b[i]); + } + for(i=0;ima&&b[i]>mb){ + k=min(a[i]-ma,b[i]-mb); + ans+=k; + a[i]=a[i]-k; + b[i]=b[i]-k; + } + else if(a[i]>ma){ + k=a[i]-ma; + ans+=k; + a[i]=ma; + } + else if(b[i]>mb){ + k=b[i]-mb; + ans+=k; + b[i]=mb; + } + } + } + cout< #include #include - -#define all(v) (v).begin(), (v).end() using namespace std; int main() { - int no_of_elements, max_per_day; - cin >> no_of_elements >> max_per_day; - - vector A(no_of_elements + 1, 0); - for(int i = 1; i <= no_of_elements; i++) - { - cin >> A[i]; - } - - sort(all(A)); - - vector sum_till(no_of_elements + 1, 0); - for(int i = 1; i <= no_of_elements; i++) - { - sum_till[i] = sum_till[i - 1] + A[i]; - } - - vector penalty(no_of_elements + 1, 0); - for(int i = 1; i <= no_of_elements; i++) - { - long long day_1 = 0, remaining_days = 0; - - if(i <= max_per_day) - { - day_1 = sum_till[i]; - } - else - { - day_1 = sum_till[i] - sum_till[i - max_per_day]; - - remaining_days = penalty[i - max_per_day] + sum_till[i - max_per_day]; - } - - penalty[i] = day_1 + remaining_days; - } - - for(int i = 1; i <= no_of_elements; i++) - { - cout << penalty[i] << " "; - } - - return 0; + ios::sync_with_stdio(false); + cin.tie(0); + + int nbSweets, maxPerDay; + cin >> nbSweets >> maxPerDay; + + vector val(nbSweets); + + for (int iSweet = 0; iSweet < nbSweets; ++iSweet) { + cin >> val[iSweet]; + } + + sort(val.begin(), val.end()); + + vector ans(nbSweets); + + long long curSum = 0; + + for (int lastTaken = 0; lastTaken < nbSweets; ++lastTaken) { + curSum += val[lastTaken]; + ans[lastTaken] = curSum; + + if (lastTaken >= maxPerDay) { + ans[lastTaken] += ans[lastTaken - maxPerDay]; + } + + cout << ans[lastTaken] << (lastTaken == nbSweets-1 ? '\n' : ' '); + } + + return 0; } -