-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckNitish.cpp
154 lines (135 loc) · 3.55 KB
/
checkNitish.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
bool visited[10001];
int a[10001],b[10001];
int n;
bool check(int x)
{
if(a[0]==x){}
else
{
if(a[0]+b[0]==x){visited[0]=true;}
else if(a[0]+b[1]==x){visited[1]=true;}
else if(a[0]+b[0]+b[1] == x) { visited[0]=visited[1]=true;}
}
for(int i=1;i<n-1;i++)
{
if(visited[i-1] ==false)
{
if(b[i-1]>x) return false;
if(b[i-1]>=x and a[i]>0) return false;
if(b[i-1]+a[i]==x)
{
visited[i-1] = true;
continue;
}
else if(b[i-1]+b[i]+a[i] == x and visited[i]==false)
{
visited[i-1]=visited[i]=true;
continue;
}
else if(b[i-1]+b[i+1]+a[i] == x )
{
visited[i-1]=visited[i+1]=true;
continue;
}
else if(b[i-1]+b[i]+b[i+1]+a[i]==x and visited[i]==false)
{
visited[i]=visited[i-1]=visited[i+1]=true;
continue;
}
else
{ //cout << "returning false " << i << endl;
//cout << b[i-1] << " " << b[i] << " " << b[i+1] << "--> " << a[i] << endl;
return false; }
}
else
{
if(a[i]==x) {continue;}
else if(a[i]+b[i] == x and visited[i] == false)
{
visited[i] = true;
continue;
}
else if(a[i]+b[i+1]==x)
{
visited[i+1]=true;
continue;
}
else if(a[i]+b[i]+b[i+1] ==x and visited[i]==false)
{
visited[i]=visited[i+1]=true;
continue;
}
else
{ //cout << "returning false " << i << endl;
//cout << b[i-1] << " " << b[i] << " " << b[i+1] << "--> " << a[i] << endl;
return false; }
}
}
int sum =0;
if(visited[n-2]==false) sum+=b[n-2];
if(visited[n-1]==false) sum+=b[n-1];
sum+=a[n-1];
if(sum!=x)
{ //cout << "returning false end sum" << endl;
return false; }
return true;
}
int main(int argc, char const *argv[])
{
int t; cin >> t;
while(t--)
{
//memset(visited,0,sizeof(visited));
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> b[i];
}
for (int i = 0; i < n; ++i)
{
cin >> a[i];
}
vector <int> v;
if(n>=2) { v.pb(a[0]); v.pb(b[0]+a[0]); v.pb(a[0]+b[1]); v.pb(a[0]+b[0]+b[1]);}
else if(n==1) {
printf("%d\n",a[0]+b[0]);
continue;
}
sort(v.begin(),v.end());
/* for (int i = 1; i < n-1; ++i)
{
mp[b[i]]++;
mp[b[i]+a[i-1]]++;
mp[b[i]+a[i]]++;
mp[b[i]+a[i+1]]++;
mp[b[i]+a[i-1]+a[i]]++;
mp[b[i]+a[i]+a[i+1]]++;
mp[b[i]+a[i-1]+a[i+1]]++;
mp[b[i]+a[i]+a[i-1]+a[i+1]]++;
}
mp[b[n-1]]++; mp[b[n-1]+a[n-1]]++;mp[b[n-1]+a[n-2]];mp[b[n-1]+a[n-1]+a[n-2]];*/
/* for(auto it = mp.begin();it!=mp.end();++it)
{
if(it->second >= n)
v.push_back(it->first);
}*/
int flag = 0;
for(int i = v.size()-1; i >= 0; i--)
{
memset(visited,0,sizeof(visited));
// cout << v[i] << endl;
if(check(v[i]))
{
flag = 1;
printf("%d\n",v[i]);
break;
}
}
if(!flag)
printf("-1\n");
}
return 0;
}