-
Notifications
You must be signed in to change notification settings - Fork 2
/
MARRAYS.cpp
76 lines (71 loc) · 1.73 KB
/
MARRAYS.cpp
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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
long int n;
cin>>n;
vector<long int>maxi(100005);
vector<long int>amaxi(100005);
vector<long int>mini(100005);
vector<long int>amini(100005);
for(long int i=0;i<n;i++)
{
long int m;
cin>>m;
long int arr[m];
for(long int j=0;j<m;j++)
{
cin>>arr[j];
}
long int cmax=0,cmin=arr[0],amax,amin;
for(long int j=0;j<m;j++)
{
if(arr[j]>=cmax)
{
cmax=arr[j];
amax=arr[(j+1)%m];
}
if(arr[j]<=cmin)
{
cmin=arr[j];
amin=arr[(j+1)%m];
}
}
maxi[i]=cmax;
amaxi[i]=amax;
mini[i]=cmin;
amini[i]=amin;
}
long int ans=0,p;
if((abs(maxi[n-1]-mini[n-2]))>abs(mini[n-1]-maxi[n-2]))
{
p=amini[n-2];
ans+=(n-1)*(abs(maxi[n-1]-mini[n-2]));
}
else
{
p=amaxi[n-2];
ans+=(n-1)*(abs(mini[n-1]-maxi[n-2]));
}
for(long int i=n-3;i>=0;i--)
{
if((abs(maxi[i]-p))>abs(mini[i]-p))
{
ans+=(i+1)*(abs(maxi[i]-p));
p=amini[i];
//cout<<ans<<"check"<<endl;
}
else
{
ans+=(i+1)*(abs(mini[i]-p));
p=amaxi[i];
//cout<<ans<<"check1"<<endl;
}
}
cout<<ans<<endl;
}
}