#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, N) for(int i = 0; i < N; i++)
#define FOR1e(i, N) for(int i = 1; i <= N; i++)
#define REP(i, M, N) for(int i = M; i < N; i++)
#define REPe(i, M, N) for(int i = M; i <= N; i++)
#define foreach(it, a) for(__typeof((a).begin()) it = (a).begin() ; it != (a).end(); it++)
#define sc(N) scanf("%d", &N)
#define scsc(M, N) scanf("%d %d", &M, &N)
#define scscsc(M, N, O) scanf("%d %d %d", &M, &N, &O)
#define all(s) s.begin(), s.end()
#define gt(s) getline(cin, s)
#define ms(a, v) memset(a, v, sizeof a)
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define ss stringstream
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int oo = 1 << 30;
const int MAX = 1e5 + 1;
const int mod = 1e9 + 7;
int dr[] = { 0, -1, 0, 1 };
int dc[] = { 1, 0, -1, 0 };
inline int Pow(int b, int p) { if (!p) return 1; int sq = Pow(b, p >> 1); sq *= sq; if (p & 1) sq *= b; return sq; }
inline int gcd(int a, int b) { if (!a) return b; return gcd(b % a, a); }
inline string toString(int x) { ss SS; SS << x; return SS.str(); }
int t, n;
ll arr[MAX], m;
ll fix(ll a) {
return (a % m + m) % m;
}
ll Kadane(){
ll max_ending_here = fix(arr[0]), max_so_far = fix(arr[0]);
ll ans = fix(arr[0]);
for (int i = 1; i < n; i++){
if (max_ending_here < 0) max_ending_here = fix(arr[i]);
else max_ending_here = fix(fix(max_ending_here) + fix(arr[i]));
//max_so_far = max(max_so_far, max_ending_here);
if (max_ending_here > max_so_far){
max_so_far = max_ending_here;
ans = max(ans, max_so_far);
}
}
return ans;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
cin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