-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_sum_maximization.js
More file actions
50 lines (46 loc) · 1.17 KB
/
Copy patharray_sum_maximization.js
File metadata and controls
50 lines (46 loc) · 1.17 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
// Time Complexity => O(nlogn)
// Space Complexity => O(1)
function runProgram(input){
let input_arr = input.trim().split("\n")
for(let i = 2; i < input_arr.length; i = i+2){
let array = input_arr[i].trim().split(" ").map(Number).sort((a,b) => a - b)
let max = 0
for(let k = 0; k < array.length; k++){
let answer = 0
let index = k
for(let j = 0; j < array.length ; j++){
if(j < index){
answer += (-1 * array[j])
}
else{
answer += (array.length - j) * array[index]
break
}
}
if(max < answer){
max = answer
}
}
console.log(max)
}
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
let read = "";
process.stdin.on("data", function (input) {
read += input;
});
process.stdin.on("end", function () {
read = read.replace(/\n$/,"")
runProgram(read);
});
process.on("SIGINT", function () {
read = read.replace(/\n$/,"")
runProgram(read);
process.exit(0);
});
runProgram(`2
1
1 2 3
5
1 6 7 1 5`)