1342A - Road To Zero - CodeForces Solution


greedy math *1000

Please click on ads to support us..

Python Code:

t=int(input())
while t!=0:
    x,y=map(int,input().split())
    a,b=map(int,input().split())
    diff=x-y
    if diff<0:
        diff=-(diff)
    p=min(x,y)
    fin=(diff*a)+(p*b)
    fin1=(x*a+y*a)
    print(min(fin,fin1))
    t=t-1

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll mod = 1e9+7;
 
// sieve of earonsthes
// const int N = 1e8;
// bool prime[N+1];
// void sieve()
// {
//     memset(prime, true, sizeof(prime));
//     prime[0] = false;
//     prime[1] = false;
//     for(ll i = 2; i*i <= N; i++)
//     {
//         if(prime[i] == true)
//         {
//             for(ll j = i*i; j <= N; j += i)
//             {
//                 prime[j] = false;
//             }
//         }
//     }
// }
 
// binary exponention
// ll solve(ll a, ll b)
// {
//     if(b == 0) return 1;
//     ll temp = solve(a, b/2);
//     if(b % 2 == 0) return (temp % mod * temp % mod) % mod;
//     else return  (((temp*temp) % mod)*a % mod) % mod;
// }
 
// sum of digits
// int sum(int n)
// {
//     int s = 0;
//     while(n)
//     {
//         s += (n % 10);
//         n /= 10;
        
//     }
    
//     return s;
// }
 
 
// next prime number
// ll next_prime(ll N)
// {
//     if (N <= 1)
//         return 2;
 
//     ll p = N;
//     bool found = false;
 
//     while (!found) {
//         p++;
 
//         if(prime[p] == true)
//             found = true;
//     }
 
//     return p;
// }
 
 


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin >> t;
    //sieve();
    while(t--)
    {
        ll x, y, a, b;
        cin >> x >> y >> a >> b;
        ll diff = abs(x-y);
        ll n = min(x, y);
        ll sum = 0;
        sum += (diff*a);
        ll a_cost = 2LL*n*a;
        ll b_cost = 1LL*n*b;
        sum += min(a_cost, b_cost);
        cout<<sum<<endl;
    }
           
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST