-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcodearena_1.cpp
More file actions
98 lines (83 loc) · 2.33 KB
/
codearena_1.cpp
File metadata and controls
98 lines (83 loc) · 2.33 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*PROBLEM STATEMENT
Exams are approaching soon and everyone tries to get ready for them. So, Omar decides to begin studying his subjects. He has N subjects and he wants to study M of them today. According to Omar, every subject has an interest level, which indicates how much Omar likes that subject. Unfortunately, Omar feels bored easily, so he decides to choose a collection of interesting and boring subjects. Actually, Omar wants to choose M subjects such that the summation S of the absolute difference between every two adjacent subjects' interest levels is maximum as possible. Omar will put the selected M subjects in a group in the same order as described in the input, and then he will proceed to calculate S.
Please help Omar and rescue him from boredom
Input:
First line of the input contains an integer T denoting the number of test cases.
First line of every test-case contains two space separated integers N and M.
Second line of every test case contains N space separated integers denoting the interest level for subject.
*Output: *
For each case, print the above described S in a separate line.
Constraints:
.
.
SAMPLE INPUT
1
5 3
100 50 3000 4000 40
SAMPLE OUTPUT
7910
Explanation
You should choose the following group in the same order that appears in the input. .
*/
#include<bits/stdc++.h>
using namespace std;
long long int result=0;
void maxcal(vector<long long int> v,int n,int m,long long int value,int jj)
{
long long int aa=0;
if(m==0)
{
if(value>=result)
{
result=value;
// return value;
return;
}
}
else{
for(int i=jj;i<n-m;i++)
{
for(int j=i+1;j<n-m+1;j++)
{
value=value+abs(v[i]-v[j]);
maxcal(v,n,m-1,value,j);
value=value-abs(v[i]-v[j]);
}
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
vector<long long int> v;
for(int i=0;i<n;i++)
{
int value;
cin>>value;
v.push_back(value);
}
if(m>1)
{
result=0;
maxcal(v,n,m-1,result,0);
}
else
{
result=INT_MIN;
for(int i=0;i<n;i++)
{
if(result<v[i])
{
result=v[i];
}
}
}
cout<<result<<endl;
}
return 0;
}