Language: C++

#include<bits/stdc++.h>
using namespace std;
namespace IO
{
    char ibuf[(1<<21)+1],obuf[(1<<21)+1],st[15],*iS,*iT,*oS=obuf,*oT=obuf+(1<<21);
	inline char Get()
	{
        return (iS==iT? (iT=(iS=ibuf)+fread(ibuf,1,(1<<21)+1,stdin),(iS==iT? EOF:*iS++)):*iS++);
    }
    inline void Flush()
	{
        fwrite(obuf,1,oS-obuf,stdout);
        oS=obuf;
    }
    inline void Put(char x)
	{
        *oS++=x;
        if(oS==oT)
			Flush();
    }
   	inline int read()
	{
	   	register int x=0;
		register char ch=Get();
    	while(ch>'9'||ch<'0')
    		ch=Get();
    	while(ch>='0'&&ch<='9')
       		x=(x<<3)+(x<<1)+(ch^48),ch=Get();
       	return x;
	}
    inline void write(int x)  
	{  
		register int top=0;
    	while(x) 
			st[++top]=(x%10)+48,x/=10;
   		while(top)
			Put(st[top--]);
	}
}
using namespace IO;
const int N=200001,M=N<<3;
struct Edge
{
    int u,v,w,Next;
    Edge(){}
    Edge(int U,int V,int W,int NEXT)
    {
        u=U,v=V,w=W,Next=NEXT;
    }
    bool operator<(Edge a)const
    {
            return w<a.w;
    }
}edge[M];
int head[N],tot,f[N],d[N];
inline void add(int x,int y,int z)
{
    edge[++tot]=Edge(x,y,z,head[x]),head[x]=tot;
}
int find(int x)
{
    return x==f[x]? x:f[x]=find(f[x]);
}
bool cmp(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx==fy)
        return false;
    if(fy>fx)
        f[fy]=fx;
    else
        f[fx]=fy;
    return true;
}
int main()
{
    register int n=read(),q=read();
    memset(d,0x3f sizeof d);
    for(register int i=1,a,b,c;i<=q;++i)
    {
        a=read(),b=read(),c=read();
        add(a,b,c);
        d[a]=min(d[a],c+1),d[b]=min(d[b],c+2);
    }
    for(register int i=0;i<n;++i)
        d[i]=min(d[i],d[(i-1+n)%n]+2);
    for(register int i=0;i<n;++i)
        d[i]=min(d[i],d[(i-1+n)%n]+2);
    for(register int i=0;i<=n;++i)
        add(i,(i+1)%n,d[i]);
    sort(edge+1,edge+1+tot);
    for(register int i=1;i<n;++i)
		f[i]=i;
    register int lt=n,ans=0;
    for(register int i=1;i<=tot&&lt>1;++i)
        if(cmp(edge[i].u, edge[i].v))
            ans+=edge[i].w,--lt;
    return printf("%lld", ans),0;
}

Compiler output:
a.cc: In function 'int IO::read()':
a.cc:23:30: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   23 |                 register int x=0;
      |                              ^
a.cc:24:31: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   24 |                 register char ch=Get();
      |                               ^~
a.cc: In function 'void IO::write(int)':
a.cc:33:30: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   33 |                 register int top=0;
      |                              ^~~
a.cc: In function 'int main()':
a.cc:77:18: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   77 |     register int n=read(),q=read();
      |                  ^
a.cc:77:27: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   77 |     register int n=read(),q=read();
      |                           ^
a.cc:78:18: error: expected ')' before 'sizeof'
   78 |     memset(d,0x3f sizeof d);
      |           ~      ^~~~~~~
      |                  )
a.cc:78:11: error: too few arguments to function 'void* memset(void*, int, size_t)'
   78 |     memset(d,0x3f sizeof d);
      |     ~~~~~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/14/cstring:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:121,
                 from a.cc:1:
/usr/include/string.h:61:14: note: declared here
   61 | extern void *memset (void *__s, int __c, size_t __n) __THROW __nonnull ((1));
      |              ^~~~~~
a.cc:79:22: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   79 |     for(register int i=1,a,b,c;i<=q;++i)
      |                      ^
a.cc:79:26: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   79 |     for(register int i=1,a,b,c;i<=q;++i)
      |                          ^
a.cc:79:28: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   79 |     for(register int i=1,a,b,c;i<=q;++i)
      |                            ^
a.cc:79:30: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   79 |     for(register int i=1,a,b,c;i<=q;++i)
      |                              ^
a.cc:85:22: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   85 |     for(register int i=0;i<n;++i)
      |                      ^
a.cc:87:22: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   87 |     for(register int i=0;i<n;++i)
      |                      ^
a.cc:89:22: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   89 |     for(register int i=0;i<=n;++i)
      |                      ^
a.cc:92:22: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   92 |     for(register int i=1;i<n;++i)
      |                      ^
a.cc:94:18: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   94 |     register int lt=n,ans=0;
      |                  ^~
a.cc:94:23: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   94 |     register int lt=n,ans=0;
      |                       ^~~
a.cc:95:22: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   95 |     for(register int i=1;i<=tot&&lt>1;++i)
      |                      ^