Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ll> vll;
#define all(v) v.begin(),v.end()
#define sz(x) (int)(x).size()
#define alr(v) v.rbegin(),v.rend()
#define pb push_back
#define S second
#define F first
#define pow2(x) (1<<(x))
#define sp(x) cout<<fixed<<setprecision(6)<<x
#define output(x) cout<<(x?"YES\n":"NO\n")
#define bp(x) __builtin_popcount(x)
//#define int long long
template<class T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
template<typename T>
void uniq(vector<T> &v) { sort(all(v)); v.resize(unique(all(v)) - v.begin()); }
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
const int N=1e5+1;
const char nl='\n';
const ll mod=1e9+7;
const ll Binf=1e18;
const ll mod1=998244353;
void solve101(){
ll n,k;
cin>>n>>k;
ll l=0,r=0;
vector<pi>v(n);
for(int i=0;i<n;i++){
cin>>v[i].F;
}
for(int j=0;j<n;j++){
cin>>v[j].S;
r+=v[j].S;
}
sort(all(v));
// debug(l,r);
ll tot=r;
for(int i=0;i<k;i++){
ll x;cin>>x;
if(i%2==0){
l+=(tot-x);
}
else{
r-=(tot-x);
}
tot=x;
}
// debug(l,r);
ll ans=0,cur=0;
for(int i=0;i<n;i++){
cur+=v[i].S;
if(cur>l){
ans+=(cur-l)*v[i].F;
l=cur;
}
if(cur>r){
ans-=(cur-r)*v[i].F;
break;
}
}
cout<<ans<<nl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
cin>>t;
for(int i=1;i<=t;i++){
// cout<<"Case #"<<i<<": ";
solve101();
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
return 0;
}
// always check for binary search >.<
// #ifndef ONLINE_JUDGE
// if (fopen("input.txt", "r"))
// {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// }
// #endif
# cook your dish here
# cook your dish here
for i in range(int(input())):
n,k=[int(i) for i in input().split()]
arr=[[int(i)] for i in input().split()]
l=input().split()
value=0
for i in range(n):
arr[i].append(int(l[i]))
value+=int(l[i])
arr.sort()
ans=[value]
move=list(map(int,input().split()))
for i in range(k):
if(i%2==0):
ans.append(ans[-1]-move[i]+1)
else:
ans.append(ans[-1]+move[i]-1)
p=0
x=max(ans[-1],ans[-2])
y=min(ans[-1],ans[-2])
index1=index2=None
for i in range(len(arr)):
p+=arr[i][1]
if(p>=y and index1==None):
index1=i
x1=p-y+1
if(p>=x):
index2=i
x2=x-p+arr[i][1]
break
if(index1==index2):
v=arr[index1][0]*(x-y+1)
else:
v=arr[index1][0]*x1 + arr[index2][0]*x2
for i in range(index1+1,index2):
v+=arr[i][0]*arr[i][1]
print(v)
#include<stdio.h>
int main()
{
long long int t;
scanf("%lld",&t);
while(t--)
{
long long int n,k;
scanf("%lld %lld",&n,&k);
long long int a[n],d[n],b[k];
for(long long int i=0;i<n;i++)
scanf("%lld",&a[i]);
for(long long int i=0;i<n;i++)
scanf("%lld",&d[i]);
for(long long int i=0;i<k;i++)
scanf("%lld",&b[i]);
long long int freq[1000010][2];
for(long long int i=0;i<1000010;i++)
{
freq[i][0]=0;
freq[i][1]=0;
}
for(long long int i=0;i<n;i++)
{
freq[a[i]][0]=a[i];
freq[a[i]][1]=d[i]+freq[a[i]][1];
}
long long int A[1000010],D[1000010],size=0;
for(long long int i=0;i<1000010;i++)
{
if(freq[i][1]!=0)
{
A[size]=freq[i][0];
D[size]=freq[i][1];
size++;
}
}
long long int ans_l,ans_r=0;
for(long long int i=0;i<k;i++)
{
if(i%2==0)
ans_l=ans_r+b[i];
else
ans_r=ans_l-b[i];
}
long long int j=n-1,final_ans=0;
while(ans_r!=0)
{
if(D[j]<=ans_r)
{
ans_r=ans_r-D[j];
ans_l=ans_l-D[j];
}
else
{
if(ans_l>D[j])
{
final_ans=final_ans+(D[j]-ans_r)*A[j];
ans_r=0;
ans_l=ans_l-D[j];
}
else
{
final_ans=final_ans+(ans_l-ans_r)*A[j];
ans_l=0;
ans_r=0;
}
}
j--;
}
while(ans_l!=0)
{
if(D[j]<=ans_l)
{
ans_l=ans_l-D[j];
final_ans=final_ans+D[j]*A[j];
}
else
{
final_ans=final_ans+ans_l*A[j];
ans_l=0;
}
j--;
}
printf("%lld\n",final_ans);
}
return 0;
}
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
char nextChar() {
char c = ' ';
try {
c = (char) br.read();
//return c;
} catch (Exception e) {
e.printStackTrace();
}
return c;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int ntc = sc.nextInt();
while(ntc -- > 0) {
int N = sc.nextInt();
int K = sc.nextInt();
long A [] = new long[N];
long D [] = new long[N];
long B [] = new long[K];
long prefix [] = new long[N];
long sum = 0;
for(int i = 0; i<N; i++) {
A[i] = sc.nextLong();
}
for(int i = 0; i<N; i++) {
D[i] = sc.nextLong();
}
for(int i = 0; i<K; i++) {
B[i] = sc.nextLong();
}
List<Pair> arr = new ArrayList<>();
for(int i =0; i<N; i++) {
Pair pair = new Pair(A[i], D[i]);
arr.add(pair);
}
Collections.sort(arr, new comparator());
for(int i =0; i<N; i++) {
Pair pair = arr.get(i);
if(i == 0) {
prefix[i] = pair.frequency;
continue;
}
prefix[i] = prefix[i - 1] + pair.frequency;
}
long start = 1;
long end = B[0];
for(int i = 1; i<K; i++) {
if((i+ 1) % 2 != 0) {
end = B[i] + start - 1;
}else {
start = end - B[i] + 1;
}
}
for(int i = 0; i<N; i++) {
if(start > end) {
break;
}
if(start > prefix[i] && end > prefix[i]) {
continue;
}
if(start <= prefix[i] && end <= prefix[i]) {
sum += (long) (end - start +1)* arr.get(i).Denomination;
start = end + 1;
continue;
}
if(start <= prefix[i] && end > prefix[i]) {
sum += (long) (prefix[i]- start +1)* arr.get(i).Denomination;
start = prefix[i] + 1;
continue;
}
}
System.out.println(sum);
}
}
static class Pair {
long Denomination, frequency;
Pair (long Denomination, long frequency)
{
this.Denomination = Denomination;
this.frequency = frequency;
}
}
static class comparator implements Comparator<Pair>{
@Override
public int compare(Pair o1, Pair o2) {
if(o1.Denomination == o2.Denomination) {
return (int) (o2.frequency - o1.frequency);
}
return (int) (o2.Denomination - o1.Denomination);
}
}
}
from __future__ import division
from math import ceil
from bisect import bisect_right as b_r
from bisect import bisect_left as b_l
for _ in range(0 , input()):
n, k = map(int , raw_input().split());ar = map(int , raw_input().split())
r = map(int , raw_input().split());a = map(int , raw_input().split())
t = []
for i in range(0, n):
t.append([ar[i],r[i]])
t.sort(key = lambda x:x[0])
x = 0
for i in range(1, k):
if i%2 == 1:
x += a[i-1]-a[i]
ll = a[k-1];tt = []
for i in t:
tt.append(i[1])
yy = x; iy =0
for i in range(len(tt)-1,-1,-1):
if tt[i] < yy:
yy -= tt[i];tt[i] = 0
else:
tt[i] -= yy;yy = 0;iy = i;break
s = 0
for i in range(iy,-1,-1):
if tt[i] < ll:
s += tt[i]*t[i][0];ll -= tt[i]
else:
s += ll*t[i][0];break
print s
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class TEST{
static void Main(){
Sol mySol =new Sol();
mySol.Solve();
}
}
class Sol{
public void Solve(){
int N,K;
long[] A,D,B;
for(;T>0;T--){
var d = ria();
N = d[0]; K = d[1];
A = rla();
D = rla();
B = rla();
Array.Sort(A,D);
long tot = 0;
for(int i=0;i<N;i++) tot += D[i];
long l = 0, r = tot;
for(int i=0;i<K;i++){
if(i % 2 == 0){
l = r - B[i];
} else {
r = l + B[i];
}
}
//Console.WriteLine("{0} {1}",l,r);
long sum = 0;
long cnt = 0;
for(int i=0;i<N;i++){
long nl = cnt;
long nr = cnt + D[i];
//Console.WriteLine("nl:{0},nr:{1}",nl,nr);
if(l <= nl && nl <= r && r <= nr){ sum += (r - nl) * A[i]; }
else if(l <= nl && nl <= nr && nr <= r){ sum += (nr - nl) * A[i]; }
else if(nl <= l && l <= r && r <= nr){ sum += (r - l) * A[i]; }
else if(nl <= l && l <= nr && nr <= r){ sum += (nr - l) * A[i]; }
cnt += D[i];
//Console.WriteLine(sum);
}
Console.WriteLine(sum);
}
}
int T;
public Sol(){
T = ri();
}
static String rs(){return Console.ReadLine();}
static int ri(){return int.Parse(Console.ReadLine());}
static long rl(){return long.Parse(Console.ReadLine());}
static double rd(){return double.Parse(Console.ReadLine());}
static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
var t, n, k, i, j, num, prev, temp, c, s, e, sum, max int
scanner.Scan()
t = toInt(scanner.Bytes())
sorted := make([]int, 1000001)
for t > 0 {
t--
c = 0
max = 0
sum = 0
scanner.Scan()
n = toInt(scanner.Bytes())
scanner.Scan()
k = toInt(scanner.Bytes())
a := make([]int, n)
for i = 0; i < n; i++ {
scanner.Scan()
a[i] = toInt(scanner.Bytes())
if a[i] > max {
max = a[i]
}
}
for i = 0; i < n; i++ {
scanner.Scan()
num = toInt(scanner.Bytes())
sorted[a[i]] += num
c += num
}
////////////
scanner.Scan()
num = toInt(scanner.Bytes())
s = max
if c != num {
for i = max; i > 0; i-- {
if sum+sorted[i] == num {
e = i
break
} else if sum+sorted[i] < num {
sum += sorted[i]
} else {
e = i
sorted[i] = num - sum
break
}
}
} else {
e = 1
}
prev = num
////////
for j = 2; j <= k; j++ {
sum = 0
if j%2 == 1 {
scanner.Scan()
num = toInt(scanner.Bytes())
temp = s
s = e
sum = prev
for i = temp; i <= s; i++ {
if sorted[i] == 0 {
continue
}
if sum-sorted[i] == num {
e = i + 1
break
} else if sum-sorted[i] > num {
sum -= sorted[i]
} else {
e = i
sorted[i] = num + sorted[i] - sum
break
}
}
prev = num
} else {
scanner.Scan()
num = toInt(scanner.Bytes())
temp = s
s = e
sum = prev
for i = temp; i >= s; i-- {
if sorted[i] == 0 {
continue
}
if sum-sorted[i] == num {
e = i - 1
break
} else if sum-sorted[i] > num {
sum -= sorted[i]
} else {
e = i
sorted[i] = num + sorted[i] - sum
break
}
}
prev = num
}
}
sum = 0
if s > e {
for i = s; i >= e; i-- {
sum += i * sorted[i]
}
} else {
for i = s; i <= e; i++ {
sum += i * sorted[i]
}
}
fmt.Println(sum)
if t != 0 {
for i = 0; i < n; i++ {
sorted[a[i]] = 0
}
}
}
}
func toInt(buf []byte) (n int) {
for _, v := range buf {
n = n*10 + int(v-'0')
}
return
}
In our experience, we suggest you solve this Game with numbers CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Game with numbers CodeChef Solution.
I hope this Game with numbers CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.
Keep Learning!