#include<bits/stdc++.h>
using namespace std;
    main(){
    int n;
    cin >> n;
    int a[n][n];
    for(int i = 1 ; i <= n ;i++){
        for(int j = 1 ; j <= n ;j++){
            cin >> a[i][j];
        }
    }
    int ans [n];
    int cnt = 0;
    bool top = false;
    for(int i = 1 ; i<= n ;i++){
        top = true;
        for(int j = 1 ; j <= n ;j++){
            if((a[i][j]==1 and top) or (a[i][j]==3 and top)){top = false;}
        }
        if(top){ans[++cnt] = i;}

    }
    cout << cnt << endl;
    sort(ans+1,ans+cnt+1);
    for(int i = 1 ; i <= cnt; i++)
        cout << ans[i] <<" ";

    }


#include<bits/stdc++.h>

using namespace std;
int n,m,a,b,i,j,t,res,d[100005];

main()
{

    cin>>n;

    res=n;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++){
            cin>>a;
            if(a==1 || a==3) {d[i]=1;res--;}
        }
    }

    cout<<res<<endl;
    for(i=0;i<n;i++){
        if(d[i]==0) cout<<i+1<<" ";
    }

}


#include<bits/stdc++.h>

using namespace std;
int n,m,a,b,i,j,t,res,d[100005];

main()
{

    cin>>n;

    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++){
            cin>>a;
            if(a==1 || a==3) {d[i]=1;}
        }
    }

    for(i=0;i<n;i++)
        if(d[i]==0) res++;
    cout<<res<<endl;
    for(i=0;i<n;i++)
        cout<<i+1<<" ";
}


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
const double esp=1e-6;
const int maxn=110;
int a[maxn][maxn];
int b[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n)){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                scanf("%d",&a[i][j]);
            }
        }
        int flag;
        int num=0;
        for(int i=0;i<n;i++){
                flag=0;
            for(int j=0;j<n;j++){
                if(a[i][j]==1||a[i][j]==3){
                    flag=1;
                    break;
                }
           }
           if(!flag) b[num++]=i;
        }
        printf("%d\n",num);
        if(num==0)
            continue;
        for(int i=0;i<num-1;i++)
            printf("%d ",b[i]+1);
            printf("%d\n",b[num-1]);
    }
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
const double esp=1e-6;
const int maxn=110;
int a[maxn][maxn];
int b[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n)){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                scanf("%d",&a[i][j]);
            }
        }
        int flag;
        int num=0;
        for(int i=0;i<n;i++){
                flag=0;
            for(int j=0;j<n;j++){
                if(a[i][j]==1||a[i][j]==3){
                    flag=1;
                    break;
                }
           }
           if(!flag) b[num++]=i;
        }
        printf("%d\n",num);
        if(num==0)
            continue;
        for(int i=0;i<num-1;i++)
            printf("%d ",b[i]+1);
            printf("%d\n",b[num-1]+1);
    }
    return 0;
}


#include <iostream>
#include<stdio.h>
#define N 100000
#include<string.h>
using namespace std;

struct node
{
    int data;
    int flag;
} A[100][100];
int main()
{
    int n,i,j,k=0,p=0,q=0;
    while(cin>>n)
    {
        k=0;
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                cin>>A[i][j].data;
                A[i][j].flag=0;
            }
        }
        if(n==1){
            printf("1\n1\n");
            continue;
        }
        for(i=0; i<n; i++)
            for(j=0; j<n; j++)
            {
                p=0;
                q=0;
                if(A[i][j].data==1)
                {
                    while(p<n)
                    {
                        A[i][p].flag=1;
                        p++;
                    }
                }
                else if(A[i][j].data==2)
                {
                    while(p<n)
                    {
                        A[p][j].flag=1;
                        p++;
                    }
                }
                else if(A[i][j].data==3)
                {
                    while(p<n&&q<n)
                    {
                        A[i][p].flag=1;
                        p++;
                            A[q][j].flag=1;
                        q++;
                    }
                }
            }
        for(i=0; i<n; i++)
            for(j=0; j<n; j++)
            {
                if(A[i][j].data==0&&A[i][j].flag==0)k++;
            }
        cout<<k<<endl;
        if(k!=0)
        {
            for(i=0; i<n; i++)
            {
                for(j=0; j<n; j++)
                {
                    if(A[i][j].data==0&&A[i][j].flag==0)
                        cout<<i+1<<" ";
                }
            }
            cout<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include<stdio.h>
#define N 100000
#include<string.h>
using namespace std;

struct node
{
    int data;
    int flag;
} A[100][100];
int main()
{
    int n,i,j,k=0,p=0,q=0;
    while(cin>>n)
    {
        k=0;
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                cin>>A[i][j].data;
                A[i][j].flag=0;
            }
        }
        for(i=0; i<n; i++)
            for(j=0; j<n; j++)
            {
                p=0;
                q=0;
                if(A[i][j].data==1)
                {
                    while(p<n)
                    {
                        A[i][p].flag=1;
                        p++;
                    }
                }
                else if(A[i][j].data==2)
                {
                    while(p<n)
                    {
                        A[p][j].flag=1;
                        p++;
                    }
                }
                else if(A[i][j].data==3)
                {
                    while(p<n&&q<n)
                    {
                        A[i][p].flag=1;
                        p++;
                            A[q][j].flag=1;
                        q++;
                    }
                }
            }
        for(i=0; i<n; i++)
            for(j=0; j<n; j++)
            {
                if(A[i][j].data==0&&A[i][j].flag==0)k++;
            }
        cout<<k<<endl;
        if(k!=0)
        {
            for(i=0; i<n; i++)
            {
                for(j=0; j<n; j++)
                {
                    if(A[i][j].data==0&&A[i][j].flag==0)
                        cout<<i+1<<" ";
                }
            }
            cout<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define endl '\n'
using namespace std;
ll powermod(ll _a,ll _b,ll _m){ll _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
ll string_to_number(string s){ll x=0; stringstream convert(s); convert>>x; return x;}
ll add(ll a,ll b){ll x = (a+b)%mod; return x; }
ll mul(ll a,ll b){ll x = (a*b)%mod; return x; }
ll sub(ll a,ll b){ll x = (a-b+mod)%mod; return x; }
ll divi(ll a,ll b){ll x = a;ll y = powermod(b,mod-2,mod);ll res = (x*y)%mod;return res;}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;

    bool flag[n+1];
    for(int i=1;i<=n;i++)flag[i]=1;

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            int x;
            cin>>x;
            if(x==3){
                flag[i]=0;
                flag[j]=0;
            }
            else if(x==1){
                flag[i]=0;
                //flag[j]=1;
            }
            else if(x==2){
                flag[j]=0;
               // flag[i]=1;
            }

        }
    }

        int cnt=0;
        for(int i=1;i<=n;i++){
            if(flag[i])cnt++;
        }

        cout<<cnt<<endl;
        for(int i=1;i<=n;i++)if(flag[i])cout<<i<<" ";

    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define endl '\n'
using namespace std;
ll powermod(ll _a,ll _b,ll _m){ll _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
ll string_to_number(string s){ll x=0; stringstream convert(s); convert>>x; return x;}
ll add(ll a,ll b){ll x = (a+b)%mod; return x; }
ll mul(ll a,ll b){ll x = (a*b)%mod; return x; }
ll sub(ll a,ll b){ll x = (a-b+mod)%mod; return x; }
ll divi(ll a,ll b){ll x = a;ll y = powermod(b,mod-2,mod);ll res = (x*y)%mod;return res;}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;

    bool flag[n+1]={0};

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            int x;
            cin>>x;
            if(x==0){
                flag[i]=1;
                flag[j]=1;
            }
            else if(x==1){
                flag[i]=0;
                flag[j]=1;
            }
            else if(x==2){
                flag[i]=1;
                flag[j]=0;
            }

        }
    }

    if(n==1){
        cout<<1<<endl;
        cout<<1<<endl;
    }
    else{
        int cnt=0;
        for(int i=1;i<=n;i++){
            if(flag[i])cnt++;
        }

        cout<<cnt<<endl;
        for(int i=1;i<=n;i++)if(flag[i])cout<<i<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define endl '\n'
using namespace std;
ll powermod(ll _a,ll _b,ll _m){ll _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
ll string_to_number(string s){ll x=0; stringstream convert(s); convert>>x; return x;}
ll add(ll a,ll b){ll x = (a+b)%mod; return x; }
ll mul(ll a,ll b){ll x = (a*b)%mod; return x; }
ll sub(ll a,ll b){ll x = (a-b+mod)%mod; return x; }
ll divi(ll a,ll b){ll x = a;ll y = powermod(b,mod-2,mod);ll res = (x*y)%mod;return res;}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;

    bool flag[n+1]={0};

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            int x;
            cin>>x;
            if(x==0){
                flag[i]=1;
                flag[j]=1;
            }
            else if(x==1){
                flag[i]=0;
                flag[j]=1;
            }
            else if(x==2){
                flag[i]=1;
                flag[j]=0;
            }

        }
    }
    int cnt=0;
    for(int i=1;i<=n;i++){
        if(flag[i])cnt++;
    }

    cout<<cnt<<endl;
    for(int i=1;i<=n;i++)if(flag[i])cout<<i<<" ";

    return 0;
}


import java.util.*;
import java.io.*;
public class Main {
    static class Scan {
        private byte[] buf=new byte[1024];
        private int index;
        private InputStream in;
        private int total;
        public Scan()
        {
            in=System.in;
        }
        public int scan()throws IOException
        {
            if(total<0)
            throw new InputMismatchException();
            if(index>=total)
            {
                index=0;
                total=in.read(buf);
                if(total<=0)
                return -1;
            }
            return buf[index++];
        }
        public int scanInt()throws IOException
        {
            int integer=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n))
            {
                if(n>='0'&&n<='9')
                {
                    integer*=10;
                    integer+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            return neg*integer;
        }
        public double scanDouble()throws IOException
        {
            double doub=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n)&&n!='.')
            {
                if(n>='0'&&n<='9')
                {
                    doub*=10;
                    doub+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            if(n=='.')
            {
                n=scan();
                double temp=1;
                while(!isWhiteSpace(n))
                {
                    if(n>='0'&&n<='9')
                    {
                        temp/=10;
                        doub+=(n-'0')*temp;
                        n=scan();
                    }
                    else throw new InputMismatchException();
                }
            }
            return doub*neg;
        }
        public String scanString()throws IOException
        {
            StringBuilder sb=new StringBuilder();
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            while(!isWhiteSpace(n))
            {
                sb.append((char)n);
                n=scan();
            }
            return sb.toString();
        }
        private boolean isWhiteSpace(int n)
        {
            if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
            return true;
            return false;
        }
    }

    public static void sort(int arr[],int l,int r) {    //sort(arr,0,n-1);
        if(l==r) {
            return;
        }
        int mid=(l+r)/2;
        sort(arr,l,mid);
        sort(arr,mid+1,r);
        merge(arr,l,mid,mid+1,r);
    }
    public static void merge(int arr[],int l1,int r1,int l2,int r2) {
        int tmp[]=new int[r2-l1+1];
        int indx1=l1,indx2=l2;
        //sorting the two halves using a tmp array
        for(int i=0;i<tmp.length;i++) {
            if(indx1>r1) {
                tmp[i]=arr[indx2];
                indx2++;
                continue;
            }
            if(indx2>r2) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            if(arr[indx1]<arr[indx2]) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            tmp[i]=arr[indx2];
            indx2++;
        }
        //Copying the elements of tmp into the main array
        for(int i=0,j=l1;i<tmp.length;i++,j++) {
            arr[j]=tmp[i];
        }
    }

    public static void sort(long arr[],int l,int r) {    //sort(arr,0,n-1);
        if(l==r) {
            return;
        }
        int mid=(l+r)/2;
        sort(arr,l,mid);
        sort(arr,mid+1,r);
        merge(arr,l,mid,mid+1,r);
    }
    public static void merge(long arr[],int l1,int r1,int l2,int r2) {
        long tmp[]=new long[r2-l1+1];
        int indx1=l1,indx2=l2;
        //sorting the two halves using a tmp array
        for(int i=0;i<tmp.length;i++) {
            if(indx1>r1) {
                tmp[i]=arr[indx2];
                indx2++;
                continue;
            }
            if(indx2>r2) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            if(arr[indx1]<arr[indx2]) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            tmp[i]=arr[indx2];
            indx2++;
        }
        //Copying the elements of tmp into the main array
        for(int i=0,j=l1;i<tmp.length;i++,j++) {
            arr[j]=tmp[i];
        }
    }

    static class DSU {
        int parent[],size[];
        public DSU(int n){
            parent=new int[n];
            size=new int[n];
            make_set(n);
        }
        public void make_set(int n) {
            for(int i=0;i<n;i++) {
                parent[i] = i;
                size[i] = 1;
            }
        }

        public void union(int a, int b) {
            a = find(a);
            b = find(b);
            if (a != b) {
                if (size[a] < size[b])
                    swap(a, b);
                parent[b] = a;
                size[a] += size[b];
            }
        }
        public int find(int v) {
            if (v == parent[v]) {
                return v;
            }
            parent[v] = find(parent[v]);
            return parent[v];
        }
        public void swap(int i,int j) {
            int tmp=size[i];
            size[i]=size[j];
            size[j]=tmp;
        }
    }

    static int n,m,u[],v[];
    static long edge[],cost[];
    static ArrayList<Integer> mst[];
    static ArrayList<Long> weight[];
    static boolean vis[],taken[];
    public static void main(String args[]) throws IOException {
        Scan input=new Scan();
        StringBuilder ans=new StringBuilder("");
        n=input.scanInt();
        long x[]=new long[n];
        long y[]=new long[n];
        for(int i=0;i<n;i++) {
            x[i]=input.scanInt();
            y[i]=input.scanInt();
        }
        cost=new long[n];
        for(int i=0;i<n;i++) {
            cost[i]=input.scanInt();
        }
        long line[]=new long[n];
        for(int i=0;i<n;i++) {
            line[i]=input.scanInt();
        }

        u=new int[(n*(n-1))/2];
        v=new int[(n*(n-1))/2];
        edge=new long[(n*(n-1))/2];
        int indx=0;
        for(int i=0;i<n;i++) {
            for(int j=i+1;j<n;j++) {
                u[indx]=i;
                v[indx]=j;
                edge[indx]=(line[i]+line[j])*(Math.abs(x[i]-x[j])+Math.abs(y[i]-y[j]));
                indx++;
            }
        }

        mst=new ArrayList[n];
        weight=new ArrayList[n];
        for(int i=0;i<n;i++) {
            mst[i]=new ArrayList<>();
            weight[i]=new ArrayList<>();
        }

        MST();

//        for(int i=0;i<mst.length;i++) {
////            System.out.print(i+"->");
//            for(int j=0;j<mst[i].size();j++) {
//                if(mst[i].get(j)<i) {
//                    continue;
//                }
//                System.out.println(i+" "+mst[i].get(j));
//            }
//        }

        u=new int[n-1];
        v=new int[n-1];
        edge=new long[n-1];
        indx=0;
        for(int i=0;i<mst.length;i++) {
            for(int j=0;j<mst[i].size();j++) {
                if(i>=mst[i].get(j)) {
                    continue;
                }
                u[indx]=i;
                v[indx]=mst[i].get(j);
                edge[indx]=weight[i].get(j);
                indx++;
            }
        }
        sort(edge,u,v,u.length);
        for(int i=0,j=u.length-1;i<j;i++,j--) {
            swap(edge,i,j);
            swap(u,i,j);
            swap(v,i,j);
        }
        boolean edge_rem[]=new boolean[u.length];
        taken=new boolean[n];
        vis=new boolean[n];
        taken[min_indx(0,-1)]=true;
        for(int i=0;i<u.length;i++) {
            indx=-1;
            mst[u[i]].remove(new Integer(v[i]));
            mst[v[i]].remove(new Integer(u[i]));
            edge_rem[i]=true;
            if(!taken(u[i],-1)) {
                indx=min_indx(u[i],-1);
            }
            else {
                indx=min_indx(v[i],-1);
            }
            taken[indx]=true;
            if(cost[indx]>edge[i]) {
                mst[u[i]].add(v[i]);
                mst[v[i]].add(u[i]);
                taken[indx]=false;
                edge_rem[i]=false;
            }
        }
        int cnt=0,cnte=0;
        long total=0;
        for(int i=0;i<n;i++) {
            if(taken[i]) {
                cnt++;
                total+=cost[i];
            }
        }
        for(int i=0;i<edge.length;i++) {
            if(!edge_rem[i]) {
                cnte++;
                total+=edge[i];
            }
        }

        ans.append(total+"\n");
        ans.append(cnt+"\n");

        for(int i=0;i<n;i++) {
            if(taken[i]) {
                ans.append((i+1)+" ");
            }
        }
        ans.append("\n");

        ans.append(cnte+"\n");
        for(int i=0;i<u.length;i++) {
            if(!edge_rem[i]) {
                ans.append((u[i]+1)+" "+(v[i]+1)+"\n");
            }
        }

        System.out.println(ans);
    }

    public static boolean taken(int root,int par) {
        if(taken[root]) {
            return true;
        }
        for(int i=0;i<mst[root].size();i++) {
            if(mst[root].get(i)==par) {
                continue;
            }
            if(taken(mst[root].get(i),root)) {
                return true;
            }
        }
        return false;
    }

    public static int min_indx(int root,int par) {
        int indx=root;
        for(int i=0;i<mst[root].size();i++) {
            if(mst[root].get(i)==par) {
                continue;
            }
            int tmp=min_indx(mst[root].get(i),root);
            if(cost[tmp]<cost[indx]) {
                indx=tmp;
            }
        }
        return indx;
    }

     public static void MST() {
        DSU dsu=new DSU(n);
        //sort
        sort(edge,u,v,u.length);
        for(int i=0;i<u.length;i++) {
//            System.out.println(u[i]+" "+v[i]+" "+edge[i]);
            if(dsu.find(u[i])==dsu.find(v[i])) {
                continue;
            }
            mst[u[i]].add(v[i]);
            mst[v[i]].add(u[i]);
            weight[u[i]].add(edge[i]);
            weight[v[i]].add(edge[i]);
            dsu.union(u[i], v[i]);
        }
    }

    static void buildMaxHeap(long arr[],int brr[],int crr[], int n) {
    for (int i = 1; i < n; i++)
    {
      // if child is bigger than parent
      if (arr[i] > arr[(i - 1) / 2])
      {
        int j = i;

        // swap child and parent until
        // parent is smaller
        while (arr[j] > arr[(j - 1) / 2])
        {
          swap(arr, j, (j - 1) / 2);
          swap(brr, j, (j - 1) / 2);
          swap(crr, j, (j - 1) / 2);
          j = (j - 1) / 2;
        }
      }
    }
  }

  static void sort(long arr[],int brr[],int crr[], int n)
  {
    buildMaxHeap(arr,brr,crr, n);

    for (int i = n - 1; i > 0; i--)
    {
      // swap value of first indexed
      // with last indexed
      swap(arr, 0, i);
      swap(brr, 0, i);
      swap(crr, 0, i);

      // maintaining heap property
      // after each swapping
      int j = 0, index;

      do
      {
        index = (2 * j + 1);

        // if left child is smaller than
        // right child point index variable
        // to right child
        if (index < (i - 1) && arr[index] < arr[index + 1])
          index++;

        // if parent is smaller than child
        // then swapping parent with child
        // having higher value
        if (index < i && arr[j] < arr[index]) {
          swap(arr, j, index);
          swap(brr, j, index);
          swap(crr, j, index);
        }

        j = index;

      } while (index < i);
    }
  }

  public static void swap(int[] a, int i, int j) {
    int temp = a[i];
    a[i]=a[j];
    a[j] = temp;
  }

  public static void swap(long[] a, int i, int j) {
    long temp = a[i];
    a[i]=a[j];
    a[j] = temp;
  }
}


import java.util.*;
import java.io.*;
public class Main {
    static class Scan {
        private byte[] buf=new byte[1024];
        private int index;
        private InputStream in;
        private int total;
        public Scan()
        {
            in=System.in;
        }
        public int scan()throws IOException
        {
            if(total<0)
            throw new InputMismatchException();
            if(index>=total)
            {
                index=0;
                total=in.read(buf);
                if(total<=0)
                return -1;
            }
            return buf[index++];
        }
        public int scanInt()throws IOException
        {
            int integer=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n))
            {
                if(n>='0'&&n<='9')
                {
                    integer*=10;
                    integer+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            return neg*integer;
        }
        public double scanDouble()throws IOException
        {
            double doub=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n)&&n!='.')
            {
                if(n>='0'&&n<='9')
                {
                    doub*=10;
                    doub+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            if(n=='.')
            {
                n=scan();
                double temp=1;
                while(!isWhiteSpace(n))
                {
                    if(n>='0'&&n<='9')
                    {
                        temp/=10;
                        doub+=(n-'0')*temp;
                        n=scan();
                    }
                    else throw new InputMismatchException();
                }
            }
            return doub*neg;
        }
        public String scanString()throws IOException
        {
            StringBuilder sb=new StringBuilder();
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            while(!isWhiteSpace(n))
            {
                sb.append((char)n);
                n=scan();
            }
            return sb.toString();
        }
        private boolean isWhiteSpace(int n)
        {
            if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
            return true;
            return false;
        }
    }

    public static void sort(int arr[],int l,int r) {    //sort(arr,0,n-1);
        if(l==r) {
            return;
        }
        int mid=(l+r)/2;
        sort(arr,l,mid);
        sort(arr,mid+1,r);
        merge(arr,l,mid,mid+1,r);
    }
    public static void merge(int arr[],int l1,int r1,int l2,int r2) {
        int tmp[]=new int[r2-l1+1];
        int indx1=l1,indx2=l2;
        //sorting the two halves using a tmp array
        for(int i=0;i<tmp.length;i++) {
            if(indx1>r1) {
                tmp[i]=arr[indx2];
                indx2++;
                continue;
            }
            if(indx2>r2) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            if(arr[indx1]<arr[indx2]) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            tmp[i]=arr[indx2];
            indx2++;
        }
        //Copying the elements of tmp into the main array
        for(int i=0,j=l1;i<tmp.length;i++,j++) {
            arr[j]=tmp[i];
        }
    }

    public static void sort(long arr[],int l,int r) {    //sort(arr,0,n-1);
        if(l==r) {
            return;
        }
        int mid=(l+r)/2;
        sort(arr,l,mid);
        sort(arr,mid+1,r);
        merge(arr,l,mid,mid+1,r);
    }
    public static void merge(long arr[],int l1,int r1,int l2,int r2) {
        long tmp[]=new long[r2-l1+1];
        int indx1=l1,indx2=l2;
        //sorting the two halves using a tmp array
        for(int i=0;i<tmp.length;i++) {
            if(indx1>r1) {
                tmp[i]=arr[indx2];
                indx2++;
                continue;
            }
            if(indx2>r2) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            if(arr[indx1]<arr[indx2]) {
                tmp[i]=arr[indx1];
                indx1++;
                continue;
            }
            tmp[i]=arr[indx2];
            indx2++;
        }
        //Copying the elements of tmp into the main array
        for(int i=0,j=l1;i<tmp.length;i++,j++) {
            arr[j]=tmp[i];
        }
    }

    static class DSU {
        int parent[],size[];
        public DSU(int n){
            parent=new int[n];
            size=new int[n];
            make_set(n);
        }
        public void make_set(int n) {
            for(int i=0;i<n;i++) {
                parent[i] = i;
                size[i] = 1;
            }
        }

        public void union(int a, int b) {
            a = find(a);
            b = find(b);
            if (a != b) {
                if (size[a] < size[b])
                    swap(a, b);
                parent[b] = a;
                size[a] += size[b];
            }
        }
        public int find(int v) {
            if (v == parent[v]) {
                return v;
            }
            parent[v] = find(parent[v]);
            return parent[v];
        }
        public void swap(int i,int j) {
            int tmp=size[i];
            size[i]=size[j];
            size[j]=tmp;
        }
    }

    static int n,m,u[],v[];
    static long edge[],cost[];
    static ArrayList<Integer> mst[];
    static ArrayList<Long> weight[];
    static boolean vis[],taken[];
    public static void main(String args[]) throws IOException {
        Scan input=new Scan();
        StringBuilder ans=new StringBuilder("");
        n=input.scanInt();
        long x[]=new long[n];
        long y[]=new long[n];
        for(int i=0;i<n;i++) {
            x[i]=input.scanInt();
            y[i]=input.scanInt();
        }
        cost=new long[n];
        for(int i=0;i<n;i++) {
            cost[i]=input.scanInt();
        }
        long line[]=new long[n];
        for(int i=0;i<n;i++) {
            line[i]=input.scanInt();
        }

        u=new int[(n*(n-1))/2];
        v=new int[(n*(n-1))/2];
        edge=new long[(n*(n-1))/2];
        int indx=0;
        for(int i=0;i<n;i++) {
            for(int j=i+1;j<n;j++) {
                u[indx]=i;
                v[indx]=j;
                edge[indx]=(line[i]+line[j])*(Math.abs(x[i]-x[j])+Math.abs(y[i]-y[j]));
                indx++;
            }
        }

        mst=new ArrayList[n];
        weight=new ArrayList[n];
        for(int i=0;i<n;i++) {
            mst[i]=new ArrayList<>();
            weight[i]=new ArrayList<>();
        }

        MST();

//        for(int i=0;i<mst.length;i++) {
////            System.out.print(i+"->");
//            for(int j=0;j<mst[i].size();j++) {
//                if(mst[i].get(j)<i) {
//                    continue;
//                }
//                System.out.println(i+" "+mst[i].get(j));
//            }
//        }

        u=new int[n-1];
        v=new int[n-1];
        edge=new long[n-1];
        indx=0;
        for(int i=0;i<mst.length;i++) {
            for(int j=0;j<mst[i].size();j++) {
                if(i>=mst[i].get(j)) {
                    continue;
                }
                u[indx]=i;
                v[indx]=mst[i].get(j);
                edge[indx]=weight[i].get(j);
                indx++;
            }
        }
        sort(edge,u,v,u.length);
        boolean edge_rem[]=new boolean[u.length];
        taken=new boolean[n];
        vis=new boolean[n];
        taken[min_indx(0,-1)]=true;
        for(int i=0;i<u.length;i++) {
            indx=-1;
            mst[u[i]].remove(new Integer(v[i]));
            mst[v[i]].remove(new Integer(u[i]));
            edge_rem[i]=true;
            if(!taken(u[i],-1)) {
                indx=min_indx(u[i],-1);
            }
            else {
                indx=min_indx(v[i],-1);
            }
            taken[indx]=true;
            if(cost[indx]>edge[i]) {
                mst[u[i]].add(v[i]);
                mst[v[i]].add(u[i]);
                taken[indx]=false;
                edge_rem[i]=false;
            }
        }
        int cnt=0,cnte=0;
        long total=0;
        for(int i=0;i<n;i++) {
            if(taken[i]) {
                cnt++;
                total+=cost[i];
            }
        }
        for(int i=0;i<edge.length;i++) {
            if(!edge_rem[i]) {
                cnte++;
                total+=edge[i];
            }
        }

        ans.append(total+"\n");
        ans.append(cnt+"\n");

        for(int i=0;i<n;i++) {
            if(taken[i]) {
                ans.append((i+1)+" ");
            }
        }
        ans.append("\n");

        ans.append(cnte+"\n");
        for(int i=0;i<u.length;i++) {
            if(!edge_rem[i]) {
                ans.append((u[i]+1)+" "+(v[i]+1)+"\n");
            }
        }

        System.out.println(ans);
    }

    public static boolean taken(int root,int par) {
        if(taken[root]) {
            return true;
        }
        for(int i=0;i<mst[root].size();i++) {
            if(mst[root].get(i)==par) {
                continue;
            }
            if(taken(mst[root].get(i),root)) {
                return true;
            }
        }
        return false;
    }

    public static int min_indx(int root,int par) {
        int indx=root;
        for(int i=0;i<mst[root].size();i++) {
            if(mst[root].get(i)==par) {
                continue;
            }
            int tmp=min_indx(mst[root].get(i),root);
            if(cost[tmp]<cost[indx]) {
                indx=tmp;
            }
        }
        return indx;
    }

     public static void MST() {
        DSU dsu=new DSU(n);
        //sort
        sort(edge,u,v,u.length);
        for(int i=0;i<u.length;i++) {
//            System.out.println(u[i]+" "+v[i]+" "+edge[i]);
            if(dsu.find(u[i])==dsu.find(v[i])) {
                continue;
            }
            mst[u[i]].add(v[i]);
            mst[v[i]].add(u[i]);
            weight[u[i]].add(edge[i]);
            weight[v[i]].add(edge[i]);
            dsu.union(u[i], v[i]);
        }
    }

    static void buildMaxHeap(long arr[],int brr[],int crr[], int n) {
    for (int i = 1; i < n; i++)
    {
      // if child is bigger than parent
      if (arr[i] > arr[(i - 1) / 2])
      {
        int j = i;

        // swap child and parent until
        // parent is smaller
        while (arr[j] > arr[(j - 1) / 2])
        {
          swap(arr, j, (j - 1) / 2);
          swap(brr, j, (j - 1) / 2);
          swap(crr, j, (j - 1) / 2);
          j = (j - 1) / 2;
        }
      }
    }
  }

  static void sort(long arr[],int brr[],int crr[], int n)
  {
    buildMaxHeap(arr,brr,crr, n);

    for (int i = n - 1; i > 0; i--)
    {
      // swap value of first indexed
      // with last indexed
      swap(arr, 0, i);
      swap(brr, 0, i);
      swap(crr, 0, i);

      // maintaining heap property
      // after each swapping
      int j = 0, index;

      do
      {
        index = (2 * j + 1);

        // if left child is smaller than
        // right child point index variable
        // to right child
        if (index < (i - 1) && arr[index] < arr[index + 1])
          index++;

        // if parent is smaller than child
        // then swapping parent with child
        // having higher value
        if (index < i && arr[j] < arr[index]) {
          swap(arr, j, index);
          swap(brr, j, index);
          swap(crr, j, index);
        }

        j = index;

      } while (index < i);
    }
  }

  public static void swap(int[] a, int i, int j) {
    int temp = a[i];
    a[i]=a[j];
    a[j] = temp;
  }

  public static void swap(long[] a, int i, int j) {
    long temp = a[i];
    a[i]=a[j];
    a[j] = temp;
  }
}


import com.sun.xml.internal.ws.util.Pool;

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args){
        InputStream  inputstream;
        OutputStream outputstream;
        inputstream = System.in;
        outputstream = System.out;
        InputReader in = new InputReader(inputstream);
        PrintWriter out = new PrintWriter(outputstream);
        Task solver = new Task();
        solver.solve(in,out);
        out.close();
    }

    static class Task{
        int n;
        int[] c;
        int[] k;
        int[] x;
        int[] y;
        int[] par;

        public void solve(InputReader in,PrintWriter out){
            n = in.nextInt();
            k = new int[n + 1];
            x = new int[n + 1];
            y = new int[n + 1];
            c = new int[n + 1];
            par = new int[n + 1];
            for(int i = 0;i < n;++i){
                x[i] = in.nextInt();
                y[i] = in.nextInt();
            }
            for(int i = 0;i < n;++i){
                c[i] = in.nextInt();
            }
            for(int i = 0;i < n;++i){
                k[i] = in.nextInt();
            }
            for(int i = 0;i <= n;++i){
                par[i] = i;
            }
            ArrayList <Tuples> edges = new ArrayList <Tuples> ();
            for(int i = 0;i < n;++i){
                for(int j = i + 1;j < n;++j){
                    int dist = (int)Math.abs(x[j] - x[i]) + (int)Math.abs(y[j] - y[i]);
                    long cost = (long)(k[i] + k[j]) * (long)dist;
                    edges.add(new Tuples(i,j,cost));
                }
            }
            for(int i = 0;i < n;++i){
                edges.add(new Tuples(n,i,c[i]));
            }
            Collections.sort(edges,new WeightCompare());
            ArrayList <Tuples> mstEdges = new ArrayList<Tuples>();
            Dsu mst = new Dsu();
            long ans = 0;
            for(Tuples E : edges){
                if(!mst.join(E.u,E.v)){
                    ans += E.c;
                    mstEdges.add(E);
                }
            }
            out.print(ans + "\n");
            ArrayList <Integer> vertices = new ArrayList<Integer>();
            edges.clear();
            for(Tuples E : mstEdges){
                ++E.u;
                ++E.v;
                if(E.u == n + 1){
                    vertices.add(E.v);
                }
                else {
                    edges.add(E);
                }
            }
            out.print(vertices.size() + "\n");
            for(int v : vertices){
                out.print(v + " ");
            }
            out.print("\n");
            out.print(edges.size() + "\n");
            for(Tuples E : edges){
                out.print(E.u + " " + E.v+ "\n");
            }
        }

        private class Tuples {
            int u,v;
            long c;
            Tuples(int u,int v,long c){
                this.u = u;
                this.v = v;
                this.c = c;
            }
        }

        private class WeightCompare implements Comparator <Tuples> {
            public int compare(Tuples x,Tuples y){
                if(x.c < y.c){
                    return  -1;
                } else if(x.c == y.c){
                    return 0;
                } else {
                    return 1;
                }
            }
        }

        private class Dsu {
            int findp(int u){
                return (u == par[u] ? u : findp(par[u]));
            }
            boolean join(int u,int v){
                u = findp(u);
                v = findp(v);
                if(u == v){
                    return true;
                }
                par[u] = v;
                return false;
            }
        }
    }

    static class InputReader{
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream){
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next(){
            while(tokenizer == null || !tokenizer.hasMoreTokens()){
                try{
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e){
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt(){
            return Integer.parseInt(next());
        }

        public long nextLong(){
            return Long.parseLong(next());
        }

        public  double nextDouble(){
            return Double.parseDouble(next());
        }

        public String nextLine(){
            String ret_str = "";
            try {
                ret_str = reader.readLine();
            } catch (IOException e){
                e.printStackTrace();
            }
            return ret_str;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int a[101010];
ll sum[101010];
ll all;
int main(int argc, char const *argv[])
{
	int n, p;
	cin >> n >> p;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		sum[i] = sum[i-1]+a[i];
		all += a[i];
	}
	ll ans = -1;
	for(int i = 1; i < n; i++){
		ans = max(ans, (sum[i]%p + (all-sum[i])%p));
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int a[101010];
ll sum[101010];
ll all;
int main(int argc, char const *argv[])
{
	int n, p;
	cin >> n >> p;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		sum[i] = sum[i-1]+a[i];
		all += a[i];
	}
	ll ans = -1;
	for(int i = 1; i < n; i++){
		ans = max(ans, (sum[i]%p + (all-sum[i])%p));
	}
	cout << ans << endl;

	return 0;
}


//958C1
#include <iostream>

using namespace std;

int N,p,a[100000],ans;
long long tot;

int main()
{
	cin >> N >> p;

	for (int i=0;i<N;++i)
	{
		cin >> a[i];
		tot+=a[i];
	}

	for (int i=1;i<N;++i)
	{
		long long totL=0;
		for (int j=0;j<i;++j)
		{
			totL += a[j];
		}

		int ansL = totL%p + (tot-totL)%p;

		if (ans < ansL)
			ans = ansL;
	}

	cout << ans;

	getchar();getchar();
	return 0;
}


//958C1
#include <iostream>

using namespace std;

int N,p,a[100000],ans;
long long tot[100000];

int main()
{
	cin >> N >> p;

	cin >> a[0];
	tot[0] = a[0];
	for (int i=1;i<N;++i)
	{
		cin >> a[i];
		tot[i] = tot[i-1] + a[i];
	}

	for (int i=0;i<N-1;++i)
	{
		int ansL = tot[i]%p + (tot[N-1]-tot[i])%p;

		if (ans < ansL)
			ans = ansL;
	}

	cout << ans;

	getchar();getchar();
	return 0;
}


//BISMILLAHHIR RAHMANIR RAHIM
#include<bits/stdc++.h>
using namespace std;
typedef long long int            ll;
typedef unsigned long long int   ull;
#define min3(a,b,c)              min(a,min(b,c))
#define F                        first
#define S                        second
#define pb                       push_back
#define mp                       make_pair
#define pi                       2*acos(0.0)
#define MOD                      998244353
int main()
{
        ll n,m,sum=0;
        cin>>n>>m;
        ll ara[n];
        for(int i=0; i<n; i++){
                cin>>ara[i];
                if(i!=0)sum+=ara[i];
        }
        ll sum1=ara[0],mx=0;
        for(int i=1; i<n-1; i++){
                ll v=sum1%m;
                ll v2=sum%m;
                mx=max(mx,(v+v2));
                sum1+=ara[i];
                sum-=ara[i];
        }
        cout<<mx<<endl;
}


//BISMILLAHHIR RAHMANIR RAHIM
#include<bits/stdc++.h>
using namespace std;
typedef long long int            ll;
typedef unsigned long long int   ull;
#define min3(a,b,c)              min(a,min(b,c))
#define F                        first
#define S                        second
#define pb                       push_back
#define mp                       make_pair
#define pi                       2*acos(0.0)
#define MOD                      998244353
int main()
{
        ll n,m,sum=0;
        cin>>n>>m;
        ll ara[n];
        for(int i=0; i<n; i++){
                cin>>ara[i];
                if(i!=0)sum+=ara[i];
        }
        ll sum1=ara[0],mx=0;
        if(n==2){
                cout<<ara[0]%m+ara[1]%m;
                return 0;
        }
        for(int i=1; i<n-1; i++){
                ll v=sum1%m;
                ll v2=sum%m;
                mx=max(mx,(v+v2));
                sum1+=ara[i];
                sum-=ara[i];
        }
        cout<<mx<<endl;
}


#include <iostream>

using namespace std;

int N, p;

const int MAX_N = 100000;

int psum[MAX_N + 5];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> N >> p;

    psum[0] = 0;

    for(int i = 1; i <= N; i++)
    {
        cin >> psum[i];
        psum[i] += psum[i-1];
    }

    int ans = 0;

    for(int i = 1; i < N; i++)
        ans = max(ans, ( psum[i] % p ) + ( psum[N] - psum[i] ) % p );

    cout << ans;

    return 0;
}


#include <iostream>

using namespace std;

int N, p;

const int MAX_N = 100000;

long long psum[MAX_N + 5];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> N >> p;

    psum[0] = 0;

    for(int i = 1; i <= N; i++)
    {
        cin >> psum[i];
        psum[i] += psum[i-1];
    }

    long long ans = 0;

    for(int i = 1; i < N; i++)
        ans = max(ans, ( psum[i] % p ) + ( psum[N] - psum[i] ) % p );

    cout << ans;

    return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 100000+10;

int arr[maxn];

int main(){
    int n,p;
    scanf("%d%d",&n,&p);
    for(int i=1;i<=n;i++){
        scanf("%d",&arr[i]);
    }
    int sign=1;
    int sum=0;
    int max_=0;
    for(int i=1;i<=n;i++){
        sum+=arr[i];
        if(sum%p>max_){
            max_=sum%p;
            sign=i;
        }
    }
    int ans=0;
    int res1=0,res2=0;
    for(int i=1;i<=sign;i++){
        res1+=arr[i];
    }
    ans+=(res1%p);
    for(int i=sign+1;i<=n;i++){
        res2+=arr[i];
    }
    ans+=(res2%p);
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 100000+10;

ll arr[maxn];

int main(){
    int n;
    ll p;
    scanf("%d%I64d",&n,&p);
    for(int i=1;i<=n;i++){
        scanf("%I64d",&arr[i]);
    }
    int sign=1;
    ll sum=0;
    ll max_=0;
    for(int i=1;i<=n;i++){
        sum+=arr[i];
        if(sum%p>max_){
            max_=sum%p;
            sign=i;
        }
    }
    ll ans=0;
    ll res1=0,res2=0;
    for(int i=1;i<=sign;i++){
        res1+=arr[i];
    }
    ans+=(res1%p);
    for(int i=sign+1;i<=n;i++){
        res2+=arr[i];
    }
    ans+=(res2%p);
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define PB push_back
#define PF push_front
#define MK make_pair
#define MT make_tuple
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define mod 1000000007

int main() {

  int N, p;
  cin >> N >> p;
  int A[N + 1], a;
  int ind = 0;
  REP(i, 1, N) {
    cin >> a;
    if (a % p) {
      A[ind] = a % p;
      ind++;
    }
  }
  LL sum = 0, s = 0;
  REP(i, 0, ind - 1) { sum = (sum + A[i]) % p; }
  if (sum == p - 1)
    cout << p - 1 << endl;
  else { // it can be sum, or sum+
    bool isfound = false;
    REP(i, 0, ind - 1) {
      s = (s + A[i]) % p;
      if (s > sum) {
        isfound = true;
        break;
      }
    }
    if (isfound)
      cout << sum + p << endl;
    else
      cout << sum << endl;
  }

  return 0;
}


#include<iostream>
using namespace std;
const int maxn=10002;
int main()
{
	long long n,p,a[maxn],b[maxn]={0},c[maxn]={0};
	cin>>n>>p;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	b[0]=a[0];
	for(int i=1;i<n;i++)
	{
		b[i]=a[i]+b[i-1];
	}
	long long s=0;
	for(int i=0;i<n-1;i++)
	{
		if((b[i]%p)+((b[n-1]-b[i])%p)>=s)
			s=(b[i]%p)+((b[n-1]-b[i])%p);
	}
	cout<<s;
	return 0;
}


#include<iostream>
using namespace std;
const int maxn=100002;
int main()
{
	long long n,p,a[maxn],b[maxn]={0},c[maxn]={0};
	cin>>n>>p;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	b[0]=a[0];
	for(int i=1;i<n;i++)
	{
		b[i]=a[i]+b[i-1];
	}
	long long s=0;
	for(int i=0;i<n-1;i++)
	{
		if((b[i]%p)+((b[n-1]-b[i])%p)>=s)
			s=(b[i]%p)+((b[n-1]-b[i])%p);
	}
	cout<<s;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main()
{
    int mx=0,x,c=0,n,i,p;
    cin>>n>>p;
    for(i=0;i<n;i++){
        cin>>x;
        c+=x;
        a[i]=a[i-1]+x;
    }
    for(i=0;i<n-1;i++){
        mx=max(mx,(c-a[i])%p+a[i]%p);
    }
    cout<<mx;
}


#include <bits/stdc++.h>
using namespace std;
long long a[100005];
int main()
{
    long long mx=0,x,c=0,n,i,p;
    cin>>n>>p;
    for(i=0;i<n;i++){
        cin>>x;
        c+=x;
        a[i]=a[i-1]+x;
    }
    for(i=0;i<n-1;i++){
        mx=max(mx,(c-a[i])%p+a[i]%p);
    }
    cout<<mx;
}


#include <bits/stdc++.h>
using namespace std;

#define N 101010

int n, mod, a[N];

long long s, t;

int main() {
	scanf("%d %d", &n, &mod);
	for (int i = 1; i <= n; i ++) scanf("%d", &a[i]), s += a[i];
	int ans = 0, now;
	for (int i = 1; i < n; i ++) {
		s -= a[i], t += a[i];
		now = (s % mod) + (t % mod);
		ans = max(now, ans);
	}
	printf("%d\n", ans);

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define pc putchar
#define gc getchar
#define pb push_back
#define ppb pop_back
#define x first
#define y second
#define f0(i, a, b) for (int i = a; i < b; i ++)
#define f1(i, a, b) for (int i = a; i <= b; i ++)
template <class T> void chkmax(T &a, T b) {if (a < b) a = b;}
template <class T> void chkmin(T &a, T b) {if (a > b) a = b;}

typedef pair <int, int> pii;
typedef pair <int, ll> pil;
typedef pair <ll, int> pli;

const int inf = 0x3f3f3f3f;
const ll linf = 2e18 + 100;
const double eps = 1e-7;

#define N 100005
int a[N], s[N];

int n, p;

int main() {
    scanf("%d %d", &n, &p);
    f1(i, 1, n) scanf("%d", &a[i]);
    f1(i, 1, n) {
        s[i] = (s[i-1] + a[i] % p) % p;
    }
    int ans = 0;
    f1(i, 1, n) {
        ans = max(ans, s[i] + (s[n] - s[i] + p) % p);
    }
    printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;

const int N = 111111;
ll a[N];
ll n, p;

int main() {
    ios::sync_with_stdio(false);
    cout.precision(10);
    cin >> n >> p;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        a[i] += a[i - 1];
    }
    int ans = 0;
    for(int i = 1; i <= n - 1; i++) {
        int sa = a[i] % p;
        int sb = (a[n] - a[i]) % p;
        ans = max(ans, sa + sb);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;

const int N = 111111;
int a[N];
int n, p;

int main() {
    ios::sync_with_stdio(false);
    cout.precision(10);
    cin >> n >> p;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        a[i] += a[i - 1];
    }
    int ans = 0;
    for(int i = 1; i <= n - 1; i++) {
        int sa = a[i] % p;
        int sb = (a[n] - a[i]) % p;
        ans = max(ans, sa + sb);
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int MX=2e5+9;
int n,p,a[MX];
ll ans,sum;
int main(){
    cin>>n>>p;
    for(int i=0;i<n;i++)scanf("%d",&a[i]),sum+=a[i];
    long long sum1=0;
    for(int i=0;i<n-1;i++){
        sum1+=a[i];
        ans=max(ans,(sum-sum1)%p+sum1%p);
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int MX=2e5+9;
int n,p,a[MX],ans,sum;
int main(){
    cin>>n>>p;
    for(int i=0;i<n;i++)scanf("%d",&a[i]),sum+=a[i];
    int sum1=0;
    for(int i=0;i<n-1;i++){
        sum1+=a[i];
        ans=max(ans,(sum-sum1)%p+sum1%p);
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int MX=2e5+9;
int n,p,a[MX],ans,sum;
int main(){
    cin>>n>>p;
    for(int i=0;i<n;i++)scanf("%d",&a[i]),sum+=a[i];
    int sum1=0;
    for(int i=0;i<n;i++){
        sum1+=a[i];
        ans=max(ans,(sum-sum1)%p+sum1%p);
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int i, j, l,s=0,so=0,a,c=0,co=0,p,b[100200],k=0,n,d[100100];
    cin>>n>>p;
    for(i=0;i<n;i++)
        {cin>>b[i];s=s+b[i];}
    for(i=0;i<n;i++){
        so=so+b[i];
        c=s-so;
        //cout<<so<<" "<<c<<endl;
        d[k++]=c%p+so%p;
    }
    sort(d,d+k);
    cout<<d[k-1];
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int N, p; cin >> N >> p;
	vector<ll> v(N), pfx(N);
	for(ll& x : v ) {
		cin >> x;
	}
	pfx[0] = v[0];
	for(int i=1;i<N;++i) {
		pfx[i] = pfx[i-1]+v[i];
	}
	ll best = 0;
	for(int i=1;i<N-1;++i) {
		best = max(best, (pfx[i-1]%p) + ((pfx[N-1]-pfx[i-1])%p));
	}
	cout << best << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int N, p; cin >> N >> p;
	vector<ll> v(N), pfx(N);
	for(ll& x : v ) {
		cin >> x;
	}
	pfx[0] = v[0];
	for(int i=1;i<N;++i) {
		pfx[i] = pfx[i-1]+v[i];
	}
	ll best = 0;
	for(int i=1;i<N;++i) {
		best = max(best, (pfx[i-1]%p) + ((pfx[N-1]-pfx[i-1])%p));
	}
	cout << best << endl;
	return 0;
}


#include<bits/stdc++.h>
#define lli long long int
#define scf(n) scanf("%lld",&n)
#define rep1(n) for(int i=1;i<=n;i++)
#define prf(n) printf("%lld",n)
#define sci(n) scanf("%d",&n)
#define pri(n) printf("%d",n)
#define prl(n) printf("%lld",n)
#define scl(n) scanf("%lld",&n)
#define nl printf("\n")
#define spc printf(" ")
#define file freopen("in.txt","rt",stdin)
#define pii pair<int,int>
using namespace std;

/*bool sortBySec(const pair<string,int> &a,const pair<string,int> &b)
{
    return (a.second > b.second);
}*/

lli arr[100009];
lli Modarr[100009];
lli sumArr[100009];

int main()
{

    lli n,p;
    scl(n);
    scl(p);

    for(int i=1;i<=n;i++)
    {
        scl(arr[i]);
        Modarr[i] = arr[i] % p;
    }

    for(int i=1;i<=n;i++)
    {
        sumArr[i] = sumArr[i-1] + Modarr[i];
    }
    lli mx = 0;
    lli pos=-1;
    for(lli i=1;i<=n-1;i++)
    {
        lli val = sumArr[i] % p;

        if(val>=mx)
        {
            mx = val;
        }
        else
        {
            pos = i;
            break;
        }
    }
    if(pos==-1)
    {
        prl(mx+arr[n]%p),nl;
        return 0;
    }
    lli s = 0;
    for(lli i=pos;i<=n;i++)
    {
        s+=Modarr[i];
    }
    s=s%p;
    prl(s+mx),nl;

    return 0;

}


#include<bits/stdc++.h>
#define lli long long int
#define scf(n) scanf("%lld",&n)
#define rep1(n) for(int i=1;i<=n;i++)
#define prf(n) printf("%lld",n)
#define sci(n) scanf("%d",&n)
#define pri(n) printf("%d",n)
#define prl(n) printf("%lld",n)
#define scl(n) scanf("%lld",&n)
#define nl printf("\n")
#define spc printf(" ")
#define file freopen("in.txt","rt",stdin)
#define pii pair<int,int>
using namespace std;

/*bool sortBySec(const pair<string,int> &a,const pair<string,int> &b)
{
    return (a.second > b.second);
}*/

lli arr[100009];
lli Modarr[100009];
lli sumArr[100009];

int main()
{

    lli n,p;
    scl(n);
    scl(p);

    for(int i=1;i<=n;i++)
    {
        scl(arr[i]);
        Modarr[i] = arr[i] % p;
    }

    for(int i=1;i<=n;i++)
    {
        sumArr[i] = sumArr[i-1] + Modarr[i];
    }
    lli mx = -1;
    for(lli i=2;i<=n-1;i++)
    {
        lli now = sumArr[i];
        lli baki = sumArr[n] - now;

        mx = max(mx,now%p+baki%p);

    }
    prl(mx),nl;
//    lli mx = 0;
//    lli pos=-1;
//    for(lli i=1;i<=n-1;i++)
//    {
//        lli val = sumArr[i] % p;
//
//        if(val>=mx)
//        {
//            mx = val;
//        }
//        else
//        {
//            pos = i;
//            break;
//        }
//    }
//    if(pos==-1)
//    {
//        prl(mx+arr[n]%p),nl;
//        return 0;
//    }
//    lli s = 0;
//    for(lli i=pos;i<=n;i++)
//    {
//        s+=Modarr[i];
//    }
//    s=s%p;
//    prl(s+mx),nl;

    return 0;

}


#include<bits/stdc++.h>
#define lli long long int
#define scf(n) scanf("%lld",&n)
#define rep1(n) for(int i=1;i<=n;i++)
#define prf(n) printf("%lld",n)
#define sci(n) scanf("%d",&n)
#define pri(n) printf("%d",n)
#define prl(n) printf("%lld",n)
#define scl(n) scanf("%lld",&n)
#define nl printf("\n")
#define spc printf(" ")
#define file freopen("in.txt","rt",stdin)
#define pii pair<int,int>
using namespace std;

/*bool sortBySec(const pair<string,int> &a,const pair<string,int> &b)
{
    return (a.second > b.second);
}*/

lli arr[100009];
lli Modarr[100009];
lli sumArr[100009];

int main()
{

    lli n,p;
    scl(n);
    scl(p);

    for(int i=1;i<=n;i++)
    {
        scl(arr[i]);
        Modarr[i] = arr[i] % p;
    }

    for(int i=1;i<=n;i++)
    {
        sumArr[i] = sumArr[i-1] + Modarr[i];
    }
    lli mx = 0;
    lli pos;
    for(lli i=1;i<=n;i++)
    {
        lli val = sumArr[i] % p;

        if(val>mx)
        {
            mx = val;
        }
        else
        {
            pos = i;
            break;
        }
    }
    lli s = 0;
    for(lli i=pos;i<=n;i++)
    {
        s+=Modarr[i];
    }
    s=s%p;
    prl(s+mx),nl;

    return 0;

}


#include<bits/stdc++.h>
#define lli long long int
#define scf(n) scanf("%lld",&n)
#define rep1(n) for(int i=1;i<=n;i++)
#define prf(n) printf("%lld",n)
#define sci(n) scanf("%d",&n)
#define pri(n) printf("%d",n)
#define prl(n) printf("%lld",n)
#define scl(n) scanf("%lld",&n)
#define nl printf("\n")
#define spc printf(" ")
#define file freopen("in.txt","rt",stdin)
#define pii pair<int,int>
using namespace std;

/*bool sortBySec(const pair<string,int> &a,const pair<string,int> &b)
{
    return (a.second > b.second);
}*/

lli arr[100009];
lli Modarr[100009];
lli sumArr[100009];

int main()
{

    lli n,p;
    scl(n);
    scl(p);

    for(int i=1;i<=n;i++)
    {
        scl(arr[i]);
        Modarr[i] = arr[i] % p;
    }

    for(int i=1;i<=n;i++)
    {
        sumArr[i] = sumArr[i-1] + Modarr[i];
    }
    lli mx = -1;
    for(lli i=1;i<=n-1;i++)
    {
        lli now = sumArr[i];
        lli baki = sumArr[n] - now;

        mx = max(mx,now%p+baki%p);

    }
    prl(mx),nl;
//    lli mx = 0;
//    lli pos=-1;
//    for(lli i=1;i<=n-1;i++)
//    {
//        lli val = sumArr[i] % p;
//
//        if(val>=mx)
//        {
//            mx = val;
//        }
//        else
//        {
//            pos = i;
//            break;
//        }
//    }
//    if(pos==-1)
//    {
//        prl(mx+arr[n]%p),nl;
//        return 0;
//    }
//    lli s = 0;
//    for(lli i=pos;i<=n;i++)
//    {
//        s+=Modarr[i];
//    }
//    s=s%p;
//    prl(s+mx),nl;

    return 0;

}


#include <iostream>
#include <cstdio>

using namespace std;

long long a[100001], s, p, mx = 0;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n >> p;
	for(int i = 1; i <= n; i++)
	{
		cin >> s;
		a[i] = a[i - 1] + s;
	}
	for(int i = 1; i < n; i++)
		if((a[n] - a[i]) % p + a[i] % p > mx)
			mx = (a[n] - a[i]) % p + a[i] % p;
	cout << mx << '\n';//123

	return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

long long a[100001], s, p, mx = 0;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n >> p;
	for(int i = 1; i <= n; i++)
	{
		cin >> s;
		a[i] = a[i - 1] + s;
	}
	for(int i = 1; i < n; i++)
		if((a[n] - a[i]) % p + a[i] % p > mx)
			mx = (a[n] - a[i]) % p + a[i] % p;
	cout << mx << '\n';

	return 0;
}


#include <iostream>
#define N 100005
#define ll long long int
using namespace std;
ll n,m;
ll vis[N],a[N],b[N];
int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>vis[i];
        vis[i]%=m;
    }
    a[0] = vis[0];
    b[n-1] = vis[n-1];
    for(int i=1;i<n;i++){
        a[i] = (vis[i]+a[i-1])%m;
    }
    for(int i=n-2;i>=0;i--){
        b[i] = (vis[i]+b[i+1])%m;
    }
    ll sum = 0;
    for(int i=0;i<n-1;i++){
        sum = max(sum,a[i]+b[i+1]);
    }
    cout<<sum<<endl;
    return 0;
}


#include <iostream>
#define N 100005
#define ll long long int
using namespace std;
ll n,m;
ll vis[N],a[N],b[N];
int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        cin>>vis[i];
        vis[i]%=m;
    }
    a[0] = vis[0];
    b[n-1] = vis[n-1];
    for(int i=1;i<n;i++){
        a[i] = (vis[i]+a[i-1])%m;
    }
    for(int i=n-2;i>=0;i--){
        b[i] = (vis[i]+b[i+1])%m;
    }
    int sum = 0;
    for(int i=0;i<n-1;i++){
        sum = max(sum,a[i]+b[i+1]);
    }
    cout<<sum<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n, p;
	cin>>n>>p;
	int a[n+9];
	long long sum = 0, sum1 = 0;
	for(int i=0; i<n; i++)
	{
		cin>>a[i];
		sum+=a[i];
	}
	long long ans = 0;
	for(int i=0; i<n-1; i++)
	{
		sum1+=a[i];
		ans = max(ans, (sum1%p)+((sum-sum1)%p));
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n, p;
	cin>>n>>p;
	int a[n];
	int sum = 0, sum1 = 0;
	for(int i=0; i<n; i++)
	{
		cin>>a[i];
		sum+=a[i];
	}
	int ans = 0;
	for(int i=0; i<n-1; i++)
	{
		sum1+=a[i];
		ans = max(ans, (sum1%p)+((sum-sum1)%p));
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n, p;
	cin>>n>>p;
	int a[n];
	int sum = 0, sum1 = 0;
	for(int i=0; i<n; i++)
	{
		cin>>a[i];
		sum+=a[i];
	}
	int ans = 0;
	for(int i=0; i<n-1; i++)
	{
		sum1+=a[i];
		ans = max(ans, (sum1%p)+(sum-sum1)%p);
	}
	cout<<ans;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define ll long long
using namespace std;
int n,m,a[1000000];
ll sum[1000020],ans;
inline int read()
{
	int x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9') ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		sum[i]=sum[i-1]+(ll)(a[i]);
	}
	for(int i=1;i<=n-1;i++)
		ans=max(ans,(sum[i])%(ll)m+(sum[n]-sum[i])%(ll)m);
	printf("%d",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int n,m,ans,a[100020],sum[100020];
inline int read()
{
	int x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9') ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		a[i]=read();
		sum[i]=sum[i-1]+a[i];
	}
	for(int i=1;i<=n-1;i++)
		ans=max(ans,sum[i]%m+(sum[n]-sum[i])%m);
	printf("%d",ans);
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;

int a[101010];
int main(){
	ios::sync_with_stdio(false);
	int n,p;
	scanf("%d%d",&n,&p);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	ll ans=0;
	ll s1=0,s2=0;
	for(int j=1;j<=n;j++)
		s1=s1+a[j];
	for(int i=1;i<n;i++){
		s1=s1-a[i];
		s2=s2+a[i];
		ans=max(ans,s1%p+s2%p);
	}
	cout<<ans<<endl;

	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;

int a[101010];
int main(){
	ios::sync_with_stdio(false);
	int n,p;
	scanf("%d%d",&n,&p);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	ll ans=0;
	for(int i=1;i<n;i++){
		ll s1=0,s2=0;
		for(int j=1;j<=i;j++)
			s1=s1+a[j];
		for(int j=i+1;j<=n;j++)
			s2=s2+a[j];
		ans=max(ans,s1%p+s2%p);
	}
	cout<<ans<<endl;

	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;

ll a[101010];
int main(){
	ios::sync_with_stdio(false);
	ll n,p;
	while(cin>>n>>p){
		for(int i=1;i<=n;i++)
			cin>>a[i];
		ll ans=-1;
		for(int i=1;i<n;i++){
			ll s1=0,s2=0;
			for(int j=1;j<=i;j++)
				s1=s1+a[j];
			for(int j=i+1;j<=n;j++)
				s2=s2+a[j];
			ans=max(ans,(s1+s2)%p);
		}
		cout<<ans<<endl;
	}

	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;

int a[101010];
int main(){
	ios::sync_with_stdio(false);
	int n,p;
	while(cin>>n>>p){
		for(int i=1;i<=n;i++)
			cin>>a[i];
		int ans=0;
		for(int i=1;i<n;i++){
			int s1=0,s2=0;
			for(int j=1;j<=i;j++)
				s1=s1+a[j];
			for(int j=i+1;j<=n;j++)
				s2=s2+a[j];
			ans=max(ans,s1%p+s2%p);
		}
		cout<<ans<<endl;
	}

	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;

int a[101010];
int main(){
	ios::sync_with_stdio(false);
	int n,p;
	while(scanf("%d%d",&n,&p)!=EOF){
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		ll ans=-1;
		for(int i=1;i<n;i++){
			ll s1=0,s2=0;
			for(int j=1;j<=i;j++)
				s1=s1+a[j];
			for(int j=i+1;j<=n;j++)
				s2=s2+a[j];
			if(s1>p)
				s1=s1%p;
			if(s2>p)
				s2=s2%p;
			ans=max(ans,s1+s2);
		}
		cout<<ans<<endl;
	}

	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;

ll a[101010];
int main(){
	ios::sync_with_stdio(false);
	ll n,p;
	while(cin>>n>>p){
		for(int i=1;i<=n;i++)
			cin>>a[i];
		ll ans=-1;
		for(int i=1;i<n;i++){
			ll s1=0,s2=0;
			for(int j=1;j<=i;j++)
				s1=s1+a[j];
			for(int j=i+1;j<=n;j++)
				s2=s2+a[j];
			ans=max(ans,s1%p+s2%p);
		}
		cout<<ans<<endl;
	}

	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N=100000;
const int INF = 0x3f3f3f3f;

ll a[101010];
int main(){
	ios::sync_with_stdio(false);
	ll n,p;
	while(cin>>n>>p){
		for(int i=1;i<=n;i++)
			cin>>a[i];
		ll ans=-1;
		for(int i=1;i<n;i++){
			ll s1=0,s2=0;
			for(ll j=1;j<=i;j++)
				s1=s1+a[j];
			for(ll j=i+1;j<=n;j++)
				s2=s2+a[j];
			ans=max(ans,s1%p+s2%p);
		}
		cout<<ans<<endl;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <fstream>
#include <set>
#include <string>
#include <cmath>
#include <map>
#include <iomanip>
using namespace std;

int main()
{
    long long number, mod;
    cin>>number>>mod;
    long long lists[number];
    long long sum[number];
    long long total=0;

    for(int a=0; a<number; a++)
    {
        cin>>lists[a];
        total+=lists[a];
        if(a==0)
            sum[a]=lists[a];
        else
            sum[a]=lists[a]+sum[a-1];
    }
    long long maximum=0;
    for(int a=1; a<number-1; a++)
    {
        long long current=sum[a]%mod+(total-sum[a])%mod;
        maximum=max(maximum, current);
    }
    cout<<maximum<<endl;
    return 0;
}


/*
ID: ycopel1
TASK: sort3
LANG: C++11
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <fstream>
#include <set>
#include <string>
#include <cmath>
#include <map>
#include <iomanip>
using namespace std;

int main()
{
    int number, mod;
    cin>>number>>mod;
    int lists[number];
    int sum[number];
    int total=0;

    for(int a=0; a<number; a++)
    {
        cin>>lists[a];
        total+=lists[a];
        if(a==0)
            sum[a]=lists[a];
        else
            sum[a]=lists[a]+sum[a-1];
    }
    int maximum=0;
    for(int a=1; a<number-1; a++)
    {
        int current=sum[a]%mod+(total-sum[a])%mod;
        maximum=max(maximum, current);
    }
    cout<<maximum<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <fstream>
#include <set>
#include <string>
#include <cmath>
#include <map>
#include <iomanip>
using namespace std;

int main()
{
    long long number, mod;
    cin>>number>>mod;
    long long lists[number];
    long long sum[number];
    long long total=0;

    for(int a=0; a<number; a++)
    {
        cin>>lists[a];
        total+=lists[a];
        if(a==0)
            sum[a]=lists[a];
        else
            sum[a]=lists[a]+sum[a-1];
    }
    long long maximum=0;
    for(int a=0; a<number-1; a++)
    {
        long long current=sum[a]%mod+(total-sum[a])%mod;
        maximum=max(maximum, current);
    }
    cout<<maximum<<endl;
    return 0;
}


#include "bits/stdc++.h"

const int MAXN = 5 * 1e5 + 1;

int dpL[100001], dpR[100001];
int sumL[100001], sumR[100001];

int main() {
    int N, M;
    std::cin >> N >> M;
    int a[N + 1];

    for (int i = 1; i <= N; i++) {
        std::cin >> a[i];
    }

    dpL[1] = a[1] % M;
    sumL[1] = a[1];

    for (int i = 2; i <= N; i++) {
        sumL[i] = sumL[i - 1] + a[i];
        dpL[i] = sumL[i] % M;
    }

    dpR[N] = a[N] % M;
    sumR[N] = a[N];
    for (int i = N - 1; i >= 1; i--) {
        sumR[i] = sumR[i + 1] + a[i];
        dpR[i] = sumR[i] % M;
    }

    dpR[0] = 0;
    dpR[N + 1] = 0;

    dpL[N + 1] = 0;
    dpL[0] = 0;

    int sum = 0;
    for (int i = 1; i <= N; i++) {
        //std::cout << dpL[i] << " " << dpR[i] << std::endl;
        sum = std::max(sum, dpL[i] + dpR[i + 1]);
    }

    std::cout << sum;

    return 0;
}


#include "bits/stdc++.h"

const int MAXN = 5 * 1e5 + 1;

long long dpL[100001], dpR[100001];
long long sumL[100001], sumR[100001];

int main() {
    long long N, M;
    std::cin >> N >> M;
    long long a[N + 1];

    for (int i = 1; i <= N; i++) {
        std::cin >> a[i];
    }

    dpL[1] = a[1] % M;
    sumL[1] = a[1];

    for (long long i = 2; i <= N; i++) {
        sumL[i] = sumL[i - 1] + a[i];
        dpL[i] = sumL[i] % M;
    }

    dpR[N] = a[N] % M;
    sumR[N] = a[N];
    for (long long i = N - 1; i >= 1; i--) {
        sumR[i] = sumR[i + 1] + a[i];
        dpR[i] = sumR[i] % M;
    }

    dpR[0] = 0;
    dpR[N + 1] = 0;

    dpL[N + 1] = 0;
    dpL[0] = 0;

    long long sum = 0;
    for (long long i = 1; i <= N; i++) {
        sum = std::max(sum, dpL[i] + dpR[i + 1]);
        sum = std::max(sum, dpR[i] + dpL[i - 1]);
    }

    std::cout << sum;

    return 0;
}


#include "bits/stdc++.h"

const int MAXN = 5 * 1e5 + 1;

int dpL[100001], dpR[100001];
int sumL[100001], sumR[100001];

int main() {
    int N, M;
    std::cin >> N >> M;
    int a[N + 1];

    for (int i = 1; i <= N; i++) {
        std::cin >> a[i];
    }

    dpL[1] = a[1] % M;
    sumL[1] = a[1];

    for (int i = 2; i <= N; i++) {
        sumL[i] = sumL[i - 1] + a[i];
        dpL[i] = sumL[i] % M;
    }

    dpR[N] = a[N] % M;
    sumR[N] = a[N];
    for (int i = N - 1; i >= 1; i--) {
        sumR[i] = sumR[i + 1] + a[i];
        dpR[i] = sumR[i] % M;
    }

    dpR[0] = 0;
    dpR[N + 1] = 0;

    dpL[N + 1] = 0;
    dpL[0] = 0;

    int sum = 0;
    for (int i = 1; i <= N; i++) {
        sum = std::max(sum, dpL[i] + dpR[i + 1]);
        sum = std::max(sum, dpR[i] + dpL[i - 1]);
    }

    std::cout << sum;

    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int a[100005] = {0};

int main() {
    int n, p;
    cin >> n >> p;
    int sum = 0, cut = 0, Max = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        sum += a[i];
    }
    for (int i = 0; i < n; ++i) {
        sum -= a[i];
        cut += a[i];
        Max = max(Max, sum%p+cut%p);
    }
    cout << Max << endl;
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int a[100005] = {0};

int main() {
    int n, p;
    cin >> n >> p;
    int sum = 0, cut = 0, Max = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        sum += a[i];
    }
    for (int i = 0; i < n-1; ++i) {
        sum -= a[i];
        cut += a[i];
        Max = max(Max, sum%p+cut%p);
    }
    cout << Max << endl;
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;
typedef long long ll;
ll a[100005] = {0};

int main() {
    int n, p;
    cin >> n >> p;
    ll sum = 0, cut = 0, Max = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        sum += a[i];
    }
    for (int i = 0; i < n-1; ++i) {
        sum -= a[i];
        cut += a[i];
        Max = max(Max, sum%p+cut%p);
    }
    cout << Max << endl;
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int a[100005] = {0};

int main() {
    int n, p;
    cin >> n >> p;
    int sum = 0, cut = 0, Max = 0;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        sum += a[i];
    }
    Max = sum % p;
    for (int i = 0; i < n; ++i) {
        sum -= a[i];
        cut += a[i];
        Max = max(Max, sum%p+cut%p);
    }
    cout << Max << endl;
    return 0;
}


#include<bits/stdc++.h>

#define N 100005

using namespace std;

int n,a[N];
long long p,sig[N];

template<typename Drake>
void read(Drake &x){
	x=0;char ch=getchar();bool f=1;
	while(!isdigit(ch)){
		if(ch=='-')f^=1;
		ch=getchar();
	}
	while(isdigit(ch)){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	x*=(2*f-1);
}

template<typename Drake>
void write(Drake x){
	if(x<0){x=-x;putchar('-');}
	if(x>9)write(x/10);
	putchar(x%10+'0');
}

template<typename Drake>
void writeln(Drake x){
	write(x);
	puts("");
}

void init(){
	read(n);read(p);
	for(int i=1;i<=n;i++)
	read(a[i]);
}

int main(){
	init();
	sig[0]=0;
	for(int i=1;i<=n;i++)
	sig[i]=sig[i-1]+a[i];
	long long ans=0,Max=0;
	for(int i=1;i<=n;i++){
		ans=sig[i]%p+(sig[n]-sig[i])%p;
		if(Max<ans)
		Max=ans;
	}
	cout<<Max<<endl;
	return 0;
}


#include<bits/stdc++.h>

#define N 10005

using namespace std;

int n,p,a[N];
long long sig[N];

template<typename Drake>
void read(Drake &x){
	x=0;char ch=getchar();bool f=1;
	while(!isdigit(ch)){
		if(ch=='-')f^=1;
		ch=getchar();
	}
	while(isdigit(ch)){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	x*=(2*f-1);
}

template<typename Drake>
void write(Drake x){
	if(x<0){x=-x;putchar('-');}
	if(x>9)write(x/10);
	putchar(x%10+'0');
}

template<typename Drake>
void writeln(Drake x){
	write(x);
	puts("");
}

void init(){
	read(n);read(p);
	for(int i=1;i<=n;i++)
	read(a[i]);
}

int main(){
	init();
	sig[0]=0;
	for(int i=1;i<=n;i++)
	sig[i]=sig[i-1]+a[i];
	long long ans=0,Max=0;
	for(int i=1;i<=n;i++){
		ans=sig[i]%p+(sig[n]-sig[i])%p;
		if(Max<ans)
		Max=ans;
	}
	cout<<Max<<endl;
	return 0;
}


///////////////////////////////////////
///// << In The Name Of Allah >> /////
/////////////////////////////////////
#include <bits/stdc++.h>
#define Salo7a ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
using namespace std;
const int N = 1e5 + 5;
ll cum[N];
int main()
{
	Salo7a;
	int n, p;
	cin >> n >> p;
	vector<int>v(n);
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
		cum[i + 1] = cum[i] + v[i];
	}

	ll mx = 0;
	for (int i = 0; i < n; i++)
		mx = max(mx, cum[i] % p + (cum[n] - cum[i]) % p);

	cout << mx << endl;
	return 0;
}


///////////////////////////////////////
///// << In The Name Of Allah >> /////
/////////////////////////////////////
#include <bits/stdc++.h>
#define Salo7a ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
using namespace std;
const int N = 1e2 + 5;
int main()
{
	Salo7a;
	int n, p, idx = 0, mx = 0; ll sum = 0;
	cin >> n >> p;
	vector<int>v(n);
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
		sum += v[i];
		if (sum % p > mx)
		{
			mx = sum % p;
			idx = i + 1;
		}
	}

	cout << accumulate(v.begin(), v.begin() + idx, 0) % p + accumulate(v.begin()+idx, v.end(), 0) % p << endl;
	return 0;
}


///////////////////////////////////////
///// << In The Name Of Allah >> /////
/////////////////////////////////////
#include <bits/stdc++.h>
#define Salo7a ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
using namespace std;
const int N = 1e2 + 5;
int main()
{
	Salo7a;
	ll n, p, idx = 0, mx = 0, sum = 0;
	cin >> n >> p;
	vector<int>v(n);
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
		sum += v[i];
		if (sum % p > mx)
		{
			mx = sum % p;
			idx = i + 1;
		}
	}

	cout << accumulate(v.begin(), v.begin() + idx, 0) % p + accumulate(v.begin()+idx, v.end(), 0) % p << endl;
	return 0;
}


///////////////////////////////////////
///// << In The Name Of Allah >> /////
/////////////////////////////////////
#include <bits/stdc++.h>
#define Salo7a ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
using namespace std;
const int N = 1e2 + 5;
int main()
{
	Salo7a;
	int n, p, sum = 0;
	cin >> n >> p;
	vector<int>v(n);
	vector<pair<int, int>>mx;
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
		sum += v[i];
		mx.push_back({ sum % p, i + 1 });
	}

	sort(allr(mx));
	int idx = mx[0].second;
	if (mx[0].second == n)
		idx = mx[1].second;

	cout << accumulate(v.begin(), v.begin() + idx, 0) % p + accumulate(v.begin()+idx, v.end(), 0) % p << endl;
	return 0;
}


///////////////////////////////////////
///// << In The Name Of Allah >> /////
/////////////////////////////////////
#include <bits/stdc++.h>
#define Salo7a ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
using namespace std;
const int N = 1e2 + 5;
int main()
{
	Salo7a;
	int n, p, sum = 0, mx = 0, idx = 0;
	cin >> n >> p;
	vector<int>v(n);
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
		sum += v[i];
		if (sum % p > mx)
		{
			mx = sum % p;
			idx = i + 1;
		}
	}

	cout << accumulate(v.begin(), v.begin() + idx, 0) % p + accumulate(v.begin()+idx, v.end(), 0) % p << endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

    int dp[100005][2];
    int a[100005];
    ll sum, sum0, ans;

int main() {

       ios_base::sync_with_stdio(false);
       cin.tie(0);

      int n, p;
       cin >> n >> p;

     for (int k=1; k<=n; k++) {
        int x;
         cin >> x;
         x%=p;
         a[k]=x;
            sum+=x;
     }

      for (int k=1; k<=n; k++) {
         sum0+=a[k];
         ans=max(ans,sum0%p+(sum-sum0)%p);
      }

       cout << ans;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

    int dp[100005][2];
    int a[100005];
    ll sum, sum0, ans;

int main() {

       ios_base::sync_with_stdio(false);
       cin.tie(0);

      int n, p;
       cin >> n >> p;

     for (int k=1; k<=n; k++) {
        int x;
         cin >> x;
         a[k]=x;
            sum+=x;
     }

      for (int k=1; k<=n; k++) {
         sum0+=a[k];
         ans=max(ans,sum0%p+(sum-sum0)%p);
      }

       cout << ans;

    return 0;
}


#include <iostream>
#define maxn 100005
using namespace std;
long long maxzyt(int a,int b)
{
  if(a>b)return a;
  else return b;
}
long long n, p, ans;
long long x[maxn];
int main() {
  cin>>n>>p;
  for (int i = 1; i <= n; i++) {
    int a;
    cin>>a;
    x[i] = x[i-1] + (long long)a;
    //cout<<x[i]<<" ";
  }
  for (int i = 2; i < n; i++) {
    ans = maxzyt(ans, (x[i] % p) + ((x[n] - x[i]) % p));
  }
  cout<<ans;
  return 0;
}


#include <iostream>
#include <cmath>
using namespace std;
const int maxn = 100005;
int n, p, ans;
long long x[maxn];
int main() {
  cin>>n>>p;
  for (int i = 1; i <= n; i++) {
    int a;
    cin>>a;
    x[i] = x[i-1] + a;
  }
  for (int i = 2; i < n; i++) {
    ans = max(ans, int((x[i] % p) + ((x[n] - x[i]) % p)));
  }
  cout<<ans;
  return 0;
}


#include <iostream>
#define maxn 100005
using namespace std;
long long maxzyt(int a,int b)
{
  if(a>b)return a;
  else return b;
}
long long n, p, ans;
long long x[maxn];
int main() {
  cin>>n>>p;
  for (int i = 1; i <= n; i++) {
    int a;
    cin>>a;
    x[i] = x[i-1] + a;
    //cout<<x[i]<<" ";
  }
  for (int i = 2; i < n; i++) {
    ans = maxzyt(ans, (x[i] % p) + ((x[n] - x[i]) % p));
  }
  cout<<ans;
  return 0;
}


#include <iostream>
#include <cmath>
long long maxzyt(int a,int b)
{
  if(a>b)return a;
  else return b;
}
using namespace std;
const int maxn = 100005;
long long n, p, ans;
long long x[maxn];
int main() {
  cin>>n>>p;
  for (int i = 1; i <= n; i++) {
    int a;
    cin>>a;
    x[i] = x[i-1] + a;
    //cout<<x[i]<<" ";
  }
  for (int i = 2; i < n; i++) {
    ans = maxzyt(ans, int((x[i] % p) + ((x[n] - x[i]) % p)));
  }
  cout<<ans;
  return 0;
}


#include <iostream>
#define maxn 100005
using namespace std;
long long maxzyt(int a,int b)
{
  if(a>b)return a;
  else return b;
}
long long n, p, ans;
long long x[maxn];
int main() {
  cin>>n>>p;
  for (int i = 1; i <= n; i++) {
    int a;
    cin>>a;
    x[i] = x[i-1] + (long long)a;
    //cout<<x[i]<<" ";
  }
  for (int i = 1; i < n; i++) {
    ans = maxzyt(ans, (x[i] % p) + ((x[n] - x[i]) % p));
  }
  cout<<ans;
  return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int n,k,p,ans;
int a[N],sum[N];
signed main(){
	scanf("%lld%lld",&n,&p);
	for (register int i=1; i<=n; ++i) scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i];
	for (register int i=1; i<n; ++i)
	{
		ans=max(ans,sum[i]%p+(sum[n]-sum[i]+p)%p);
	}
	printf("%lld\n",ans);
return 0;
}


#include <iostream>
#include <vector>
#include <ostream>
using namespace std;

template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
	for(int i=0;i<v.size();i++)os<<v[i]<<" ";
		return os;
}

void solution(int n, int p, vector<int>v){
	int max=0;
    for(int q=0;q<v.size();q++){
        int sum1=0,sum2=0,s=0;
        for(int i=0;i<q;i++){
            sum1 += v[i]%p;
        }
        for(int i=q;i<v.size();i++){
            sum2 += v[i]%p;
        }
        s= sum1%p + sum2%p;
        if(s>max)max=s;
    }
	cout << max << endl;
}

int readint(){
	int x;
	scanf("%d", &x);
	return x;
}

int main(int argc, char const *argv[])
{
	vector<int> v;
	int n, p;
	n = readint();
	p = readint();
	for(int i=0;i<n;i++){
		v.push_back(readint());
	}
	solution(n,p,v);
	return 0;
}


#include <iostream>
#include <vector>
#include <ostream>
using namespace std;

template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
	for(int i=0;i<v.size();i++)os<<v[i]<<" ";
		return os;
}

void solution(int n, int p, vector<int>v){
	int max=0;
    for(int q=0;q<v.size();q++){
        int sum1=0,sum2=0,s=0;
        for(int i=0;i<q;i++){
            sum1 += v[i];
        }
        for(int i=q;i<v.size();i++){
            sum2 += v[i];
        }
        s= sum1%p + sum2%p;
        if(s>max)max=s;
    }
	cout << max << endl;
}

int readint(){
	int x;
	scanf("%d", &x);
	return x;
}

int main(int argc, char const *argv[])
{
	vector<int> v;
	int n, p;
	n = readint();
	p = readint();
	for(int i=0;i<n;i++){
		v.push_back(readint());
	}
	solution(n,p,v);
	return 0;
}


#include <iostream>
#include <vector>
#include <ostream>
using namespace std;

template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
	for(int i=0;i<v.size();i++)os<<v[i]<<" ";
		return os;
}

void solution(int n, int p, vector<int>v){
	long max=0;
    for(int q=0;q<v.size();q++){
        long sum1=0,sum2=0,s=0;
        for(int i=0;i<q;i++){
            sum1 += v[i];
        }
        for(int i=q;i<v.size();i++){
            sum2 += v[i];
        }
        s= sum1%p + sum2%p;
        if(s>max)max=s;
    }
	cout << max << endl;
}

int readint(){
	int x;
	scanf("%d", &x);
	return x;
}

int main(int argc, char const *argv[])
{
	vector<int> v;
	int n, p;
	n = readint();
	p = readint();
	for(int i=0;i<n;i++){
		v.push_back(readint());
	}
	solution(n,p,v);
	return 0;
}


#include <iostream>
#include <vector>
#include <ostream>
using namespace std;

template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
	for(int i=0;i<v.size();i++)os<<v[i]<<" ";
		return os;
}

void solution(int n, int p, vector<int>v){
	long long max=0, sum=0, sum1=0,sum2=0,s=0;
    for(int i=0;i<v.size();i++){
    	sum += v[i];
    }
    sum2=sum;
    max = sum2%p;
    for(int i=0;i<v.size();i++){
    	sum1 += v[i];
    	sum2 -= v[i];
    	s = sum1%p + sum2%p;
    	if(s>max)max=s;
    }
	cout << max << endl;
}

int readint(){
	int x;
	scanf("%d", &x);
	return x;
}

int main(int argc, char const *argv[])
{
	vector<int> v;
	int n, p;
	n = readint();
	p = readint();
	for(int i=0;i<n;i++){
		v.push_back(readint());
	}
	solution(n,p,v);
	return 0;
}


#include <iostream>
#include <vector>
#include <ostream>
using namespace std;

template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
	for(int i=0;i<v.size();i++)os<<v[i]<<" ";
		return os;
}

void solution(int n, int p, vector<int>v){
	long max=0;
    for(int q=0;q<v.size();q++){
        long sum1=0,sum2=0,s=0;
        for(int i=0;i<q;i++){
            sum1 += v[i]%p;
        }
        for(int i=q;i<v.size();i++){
            sum2 += v[i]%p;
        }
        s= sum1%p + sum2%p;
        if(s>max)max=s;
    }
	cout << max << endl;
}

int readint(){
	int x;
	scanf("%d", &x);
	return x;
}

int main(int argc, char const *argv[])
{
	vector<int> v;
	int n, p;
	n = readint();
	p = readint();
	for(int i=0;i<n;i++){
		v.push_back(readint());
	}
	solution(n,p,v);
	return 0;
}


#include <iostream>
#include <vector>
#include <ostream>
using namespace std;

template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
	for(int i=0;i<v.size();i++)os<<v[i]<<" ";
		return os;
}

void solution(int n, int p, vector<int>v){
	long long max=0;
    for(int q=0;q<v.size();q++){
        long long sum1=0,sum2=0,s=0;
        for(int i=0;i<q;i++){
            sum1 += v[i];
        }
        for(int i=q;i<v.size();i++){
            sum2 += v[i];
        }
        s= sum1%p + sum2%p;
        if(s>max)max=s;
    }
	cout << max << endl;
}

int readint(){
	int x;
	scanf("%d", &x);
	return x;
}

int main(int argc, char const *argv[])
{
	vector<int> v;
	int n, p;
	n = readint();
	p = readint();
	for(int i=0;i<n;i++){
		v.push_back(readint());
	}
	solution(n,p,v);
	return 0;
}


#include <iostream>
#include <vector>
#include <ostream>
using namespace std;

template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
	for(int i=0;i<v.size();i++)os<<v[i]<<" ";
		return os;
}

void solution(int n, int p, vector<int>v){
	long max=0, sum=0, sum1=0,sum2=0,s=0;
    for(int i=0;i<v.size();i++){
    	sum += v[i];
    }
    sum2=sum;
    max = sum2%p;
    for(int i=0;i<v.size();i++){
    	sum1 += v[i];
    	sum2 -= v[i];
    	s = sum1%p + sum2%p;
    	if(s>max)max=s;
    }
	cout << max << endl;
}

int readint(){
	int x;
	scanf("%d", &x);
	return x;
}

int main(int argc, char const *argv[])
{
	vector<int> v;
	int n, p;
	n = readint();
	p = readint();
	for(int i=0;i<n;i++){
		v.push_back(readint());
	}
	solution(n,p,v);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    queue<int> nums;
    int n, mod;
    int sum1 = 0;
    int sum2 = 0;
    cin >> n >> mod;
    for(int i = 0; i < n; i++){
        int k;
        cin >> k;
        nums.push(k);
    }

    sum1 = nums.front();
    nums.pop();
    sum2 = sum1;
    for(int i = 0; i < n - 1; i++){
        sum2 += nums.front();
        nums.pop();
        if(sum1 % mod < sum2 % mod) sum1 = sum2;
    }
    cout << (sum1 % mod) + ((sum2 - sum1) % mod) << "\n";
}


#include<bits/stdc++.h>
using namespace std;

int main(){
    queue<int> nums;
    int n, mod;
    long long int sum1 = 0;
    long long int sum2 = 0;
    cin >> n >> mod;
    for(int i = 0; i < n; i++){
        int k;
        cin >> k;
        nums.push(k);
    }

    sum1 = nums.front();
    nums.pop();
    sum2 = sum1;
    for(int i = 0; i < n - 1; i++){
        sum2 += nums.front();
        nums.pop();
        if(sum1 % mod < sum2 % mod) sum1 = sum2;
    }
    cout << (sum1 % mod) + ((sum2 - sum1) % mod) << "\n";
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back

using namespace std;

ll gcd(ll a,ll b){
    if(b==0) return a;
    return gcd(b,a%b);
}

int mxor(int x, int y){
    return (x | y) & (~x | ~y);
}

string lower(string &s){
    for(int i=0;i<s.length();i++){
        if(s[i]<='Z')
        s[i]=s[i]+32;
    }
    return s;
}

int main(){

    int n,p;
    cin>>n>>p;
    int a[n];
    ll sum1=0;
    ll sum2=0;
    for(int i=0;i<n;i++){
        cin>>a[i];
        sum2+=a[i];
    }
    sum1=a[0];
    sum2=(sum2-a[0]);

    ll mx=max(0ll,(sum2%p)+(sum1%p));

    for(int i=1;i<n-1;i++){
        //cout<<sum1<<" "<<sum2<<" ";
        sum1=(sum1+a[i]);
        sum2=(sum2-a[i]);
        mx=max(mx,(sum2%p)+(sum1%p));
    }
    //cout<<endl;
    cout<<mx;

    return 0;
}
/*
*/


#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e5+5;
int n,mod,maxsum=0,sum[N],a[N],tot;
int main()
{
	scanf("%d%d",&n,&mod);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		sum[i]=sum[i-1]+a[i];
	}
	tot=sum[n];
	for(int i=1;i<n;i++)
	{
		int pos=(tot-sum[i])%mod+sum[i]%mod;
		maxsum=max(maxsum,pos);
	}
	printf("%d",maxsum);
	return 0;
}
/*

*/


#include<cstdio>
#include<algorithm>
using namespace std;
typedef unsigned long long ULL;
const int N=1e5+5;
ULL n,mod,sum[N],a[N],tot;
ULL maxsum,pos;
int main()
{
	scanf("%llu%llu",&n,&mod);
	for(int i=1;i<=n;i++)
	{
		scanf("%llu",&a[i]);
		sum[i]=sum[i-1]+a[i];
	}
	tot=sum[n];
	for(int i=1;i<n;i++)
	{
		ULL pos=(tot-sum[i])%mod+sum[i]%mod;
		maxsum=max(maxsum,pos);
	}
	printf("%llu",maxsum);
	return 0;
}
/*

*/


#include<cstdio>
#include<algorithm>
using namespace std;
typedef unsigned long long ULL;
const int N=1e5+5;
int n,mod,sum[N],a[N],tot;
ULL maxsum,pos;
int main()
{
	scanf("%d%d",&n,&mod);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		sum[i]=sum[i-1]+a[i];
	}
	tot=sum[n];
	for(int i=1;i<n;i++)
	{
		ULL pos=(tot-sum[i])%mod+sum[i]%mod;
		maxsum=max(maxsum,pos);
	}
	printf("%llu",maxsum);
	return 0;
}
/*

*/


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    long n, p, x;
    long i, j;
    long s, ms;
    long s1, s2;

    cin >> n >> p;

    vector<long> a(n);

    i = 0;
    s2=0;
    while(i<n)
    {
        cin >> x;
        a[i] = x%p;
        if(i>0)
            s2 += x;
            i++;
    }
    s1 = a[0];
    ms = s1%p + s2%p;
    i=1;
    while(i<n-1)
    {
        s1 += a[i];
        s2 -= a[i];
        s = s1%p + s2%p;
        if(s>ms)
            ms = s;
        i++;
    }

    cout << ms << endl;

    return 0;
}
// 1534614889378


#include<iostream>
using namespace std;

int main(){
	int n, m, a[112345], t, max = 0;

	cin >> n >> m;
	for(int i = 0; i < n; i++){
		cin >> t;
		a[i] = t % m;
	}

	int soma1,soma2;
	for(int i = 1; i < n; i++){
		soma1 =0;soma2 =0;
		int j;
		for(j = 0; j < i; j++){
			soma1 += a[j];
		}
		soma1 %= m;

		for(j; j < n; j++){
			soma2 += a[j];
		}
		soma2 %= m;

		if((soma1+soma2) > max)
			max = soma1 + soma2;

	}
	cout << max << endl;
	return 0;
}

// 1534610213582


#include <iostream>

using namespace std;

int main()
{
    int a, b;
    cin >> a >> b;
    long long int d = 0;
    long long int c, z[a];
    long long int s1, s2, daux;

    for(int i = 0; i < a; i++)
    {
        cin >> c;
        if(i == 0)
            z[0] = c;
        else
            z[i] = c + z[i-1];

    }

    for(int i = 0; i < (a-1); i++) {
        s1 = z[i];
        s2 = z[a-1] - s1;
        daux = s2%b + s1%b;
        if(daux > d)
            d = daux;
        //cout << i << '\t' << s1 << '\t' << s2 << '\t' << daux << '\t' << d << endl;
    }
    cout << d << endl;

    return 0;
}

// 1534615763543


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
int main() {
    int n, p;
    cin >> n >> p;

    vector<ll> v(n);
    vector<ll> s(n+1, 0);
    for(int i = 0; i < n; ++i) cin >> v[i];

    for (int i = 1; i <= n; ++i) s[i] = s[i-1] + v[i-1];

    ll r = 0;
    for (int i = 1; i < n; ++i) {
        r = max(r, (s[i] % p) + (s[n] - s[i]) % p);
    }

    cout << r << endl;

    return 0;
}

// 1534610701730


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {

	ll n, p;
	cin >> n >> p;
	ll aux;
	ll S1 = 0;
	ll S2 = 0;
	bool colocaPrimeiro = true;

	for(int i = 0; i < n; i++) {
		cin >> aux;

		if(colocaPrimeiro) {
			if(((S1 + aux) % p) >= (S1 % p)) {
				S1 += aux;
				//cout << "colocando " << aux << " em S1" << endl;
			}
			else {
				S2 += aux;
			//	cout << "colocando " << aux << " em S2" << endl;
				colocaPrimeiro = false;
			}
		}
		else {
			S2 += aux;
		//	cout << "colocando " << aux << " em S2" << endl;
		}
	}
	//cout << "S1 = " << S1 << " S2 = " << S2 << endl;
	cout << (S1 % p) + (S2 % p) << endl;

	return 0;
}
// 1534610485954


 #include<bits/stdc++.h>
using namespace std ;
long long a[100001] ,b[100001],count1[1000001];
long long graph[1001][1001] ;
bool check[1000001];
int main() {
    long long n , m  ;
    cin >> n >> m ;
    for(int  i =1 ; i <= n ; i++ ) {
            cin >> a[i] ;

    }
    int   dem = 0 ;
    long long  sum = 0 ;
    long long  sum1 = 0 ;
    int max = 0 ;
    long long  res ;
    long long  temp , temp1 ;
    int   count = n/2;
    for(int  i = n/2+1; i <= n  ; i++ ) {
            sum1+=a[i];
    }
    temp = sum1 ;
    for(int  i = 1 ; i <= n/2 ; i++) {
        sum+=a[i] ;
     }
     temp1 = sum ;
    vuong: dem++;
    int  k = sum%m + sum1%m ;
    if(k > max ){
        max = k ;
    }
    if(count == 1)
    {
        goto res ;
    }
    if(dem >= 2 )
    {
        //cout << sum1 <<" "<< sum << endl;
        sum1+=a[count];
        sum-=a[count];
        count--;
    }

     goto vuong ;
     res :
    int   max1 = 0 ;
    int  count2 = n/2+1;
    while(count2 <= n-1){
         temp1 += a[count2];
         temp -= a[count2] ;
     //    cout << temp1 << " " << temp << endl;
         int  res = temp%m + temp1%m;
         if(res > max ){
            max = res ;
         }
         count2++ ;
    }
    cout << max << endl;
}


#include<bits/stdc++.h>
using namespace std ;
long long a[100001] ,b[100001],count1[1000001];
long long graph[1001][1001] ;
bool check[1000001];
int main() {
    long long n , m  ;
    cin >> n >> m ;
    for(int i =1 ; i <= n ; i++ ) {
            cin >> a[i] ;

    }
   // sort(a+1,a+n+1) ;
    int dem = 0 ;
    int sum = 0 ;
    int sum1 = 0 ;
    int max = 0 ;
    int res ;
    int count = n/2;
    for(int i = n/2+1; i <= n  ; i++ ) {
            sum1+=a[i];
    }
    for(int i = 1 ; i <= n/2 ; i++) {
        sum+=a[i] ;
     }
    vuong: dem++;
    int k = sum%m + sum1%m ;
    if(k > max ){
        max = k ;
    }

    if(dem >= 2 )
    {
        sum1+=a[count];
       // count--;
        sum-=a[count];
        count--;
    }
    if(count == 1) {
        goto res ;
    }
     goto vuong ;
     res : cout << max << endl;

}


#include<bits/stdc++.h>
using namespace std ;
long long a[100001] ,b[100001],count1[1000001];
long long graph[1001][1001] ;
bool check[1000001];
int main() {
    long long n , m  ;
    cin >> n >> m ;
    for(int  i =1 ; i <= n ; i++ ) {
            cin >> a[i] ;

    }
    int   dem = 0 ;
    long long  sum = 0 ;
    long long  sum1 = 0 ;
    int max = 0 ;
    long long  res ;
    long long  temp , temp1 ;
    int   count = n/2;
    for(int  i = n/2+1; i <= n  ; i++ ) {
            sum1+=a[i];
    }
    temp = sum1 ;
    for(int  i = 1 ; i <= n/2 ; i++) {
        sum+=a[i] ;
     }
     temp1 = sum ;
    vuong: dem++;
    int  k = sum%m + sum1%m ;
    if(k > max ){
        max = k ;
    }

    if(dem >= 2 )
    {
        //cout << sum1 <<" "<< sum << endl;
        sum1+=a[count];
        sum-=a[count];
        count--;
    }
    if(count == 2) {
        int  k = sum%m + sum1%m ;
        if(k > max ){
            max = k ;
        }
        goto res ;
    }
     goto vuong ;
     res :
    int   max1 = 0 ;
    int  count2 = n/2+1;
    while(count2 <= n-1){
         temp1 += a[count2];
         temp -= a[count2] ;
     //    cout << temp1 << " " << temp << endl;
         int  res = temp%m + temp1%m;
         if(res > max ){
            max = res ;
         }
         count2++ ;
    }
    cout << max << endl;

}


#include<bits/stdc++.h>
using namespace std ;
long long a[100001] ,b[100001],count1[1000001];
long long graph[1001][1001] ;
bool check[1000001];
int main() {
    long long n , m  ;
    cin >> n >> m ;
    for(int i =1 ; i <= n ; i++ ) {
            cin >> a[i] ;

    }
   // sort(a+1,a+n+1) ;
    int dem = 0 ;
    int sum = 0 ;
    int sum1 = 0 ;
    int max = 0 ;
    int res ;
    int temp , temp1 ;
    int count = n/2;
    for(int i = n/2+1; i <= n  ; i++ ) {
            sum1+=a[i];
    }
    temp = sum1 ;
    for(int i = 1 ; i <= n/2 ; i++) {
        sum+=a[i] ;
     }
     temp1 = sum ;
    vuong: dem++;
    int k = sum%m + sum1%m ;
    if(k > max ){
        max = k ;
    }

    if(dem >= 2 )
    {
        sum1+=a[count];
       // count--;
        sum-=a[count];
        count--;
    }
    if(count == 1) {
        goto res ;
    }
     goto vuong ;
     res :
    int max1 = 0 ;
    int count2 = n/2+1;
    while(count2 <= n-1){
         temp1 += a[count2];
         temp -= a[count2] ;
         int res = temp%m + temp1%m;
         if(res > max ){
            max = res ;
         }
         count2++ ;
    }
    cout << max << endl;

}


#include<bits/stdc++.h>
using namespace std ;
long long a[100001] ,b[100001],count1[1000001];
long long graph[1001][1001] ;
bool check[1000001];
int main() {
    long long n , m  ;
    cin >> n >> m ;
    for(long long  i =1 ; i <= n ; i++ ) {
            cin >> a[i] ;

    }
    long long  dem = 0 ;
    long long  sum = 0 ;
    long long  sum1 = 0 ;
    long long  max = 0 ;
    long long  res ;
    long long  temp , temp1 ;
    long long  count = n/2;
    for(long long  i = n/2+1; i <= n  ; i++ ) {
            sum1+=a[i];
    }
    temp = sum1 ;
    for(long long  i = 1 ; i <= n/2 ; i++) {
        sum+=a[i] ;
     }
     temp1 = sum ;
    vuong: dem++;
    long long  k = sum%m + sum1%m ;
    if(k > max ){
        max = k ;
    }

    if(dem >= 2 )
    {
        //cout << sum1 <<" "<< sum << endl;
        sum1+=a[count];
        sum-=a[count];
        count--;
    }
    if(count == 2) {
        long long  k = sum%m + sum1%m ;
        if(k > max ){
            max = k ;
        }
        goto res ;
    }
     goto vuong ;
     res :
    long long  max1 = 0 ;
    long long  count2 = n/2+1;
    while(count2 <= n-1){
         temp1 += a[count2];
         temp -= a[count2] ;
     //    cout << temp1 << " " << temp << endl;
         long long  res = temp%m + temp1%m;
         if(res > max ){
            max = res ;
         }
         count2++ ;
    }
    cout << max << endl;

}